This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
forked from ubbop42/optimizely-dart
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.dart
123 lines (109 loc) · 3.34 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:optimizely_dart/optimizely_dart.dart';
Future<void> main() async {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _priceFilterFlag = 'unknown';
String _minPriceVariable = 'unknown';
final OptimizelyPlugin optimizelyPlugin = OptimizelyPlugin();
@override
void initState() {
super.initState();
setupOptimizelyAsync();
}
void setupOptimizely() async {
// Uses the Optimizely example:
// https://docs.developers.optimizely.com/full-stack/docs/example-datafile
final dataFile =
await DefaultAssetBundle.of(context).loadString('assets/datafile.json');
await optimizelyPlugin.initOptimizelyManager(
'your_optimizely_sdk_key',
dataFile,
);
}
void setupOptimizelyAsync() async {
await optimizelyPlugin.initOptimizelyManagerAsync(
'your_optimizely_sdk_key',
);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> getPriceFilterFlag() async {
String priceFilterFlag;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
bool? featureEnabled = await optimizelyPlugin.isFeatureEnabled(
'price_filter',
);
priceFilterFlag = 'price_filter feature is $featureEnabled.';
} on PlatformException catch (e) {
priceFilterFlag = "Failed to get feature: '${e.message}'.";
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_priceFilterFlag = priceFilterFlag;
});
}
Future<void> getPriceFilterMinPrice() async {
String minPriceVariable;
Map<String, dynamic> variables;
try {
variables = await optimizelyPlugin.getAllFeatureVariables(
'price_filter',
);
int? minPrice = variables['min_price'];
minPriceVariable = "min_price variable is: ${minPrice.toString()}.";
} catch (e) {
minPriceVariable = "Failed to get min_price variable from feature: '$e'.";
}
if (!mounted) return;
setState(() {
_minPriceVariable = minPriceVariable;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Optimizely Example App'),
),
body: SafeArea(
child: Column(
children: [
SizedBox(height: 32),
Center(
child: Text(_priceFilterFlag),
),
ElevatedButton(
child: Text('Get Price Filter Flag'),
onPressed: () {
getPriceFilterFlag();
},
),
SizedBox(height: 16),
Center(
child: Text(_minPriceVariable),
),
ElevatedButton(
child: Text('Get Price Filter Min Price'),
onPressed: () {
getPriceFilterMinPrice();
},
)
],
),
),
),
);
}
}