1- import 'dart:io' ;
2-
3- import 'package:flutter/material.dart' ;
41import 'dart:async' ;
5- import 'dart:math' ;
62import 'dart:convert' ;
3+ import 'dart:io' ;
4+ import 'dart:math' ;
75
8- import 'package:countly_flutter_np/countly_flutter.dart' ;
6+ // ignore: depend_on_referenced_packages
97import 'package:countly_flutter_np/countly_config.dart' ;
10-
8+ // ignore: depend_on_referenced_packages
9+ import 'package:countly_flutter_np/countly_flutter.dart' ;
10+ import 'package:flutter/material.dart' ;
1111
1212final navigatorKey = GlobalKey <NavigatorState >();
13+
1314/// This or a similar call needs to added to catch and report Dart Errors to Countly,
14- /// You need to run app inside a Zone
15+ /// You need to run the app inside a Zone
1516/// and provide the [Countly.recordDartError] callback for [onError()]
1617void main () {
1718 runZonedGuarded <Future <void >>(() async {
18- runApp (MaterialApp (
19- home: MyApp (),
20- navigatorKey: navigatorKey, // Setting a global key for navigator
21- ),);
19+ runApp (
20+ MaterialApp (
21+ home: const MyApp (),
22+ navigatorKey: navigatorKey, // Setting a global key for navigator
23+ ),
24+ );
2225 }, Countly .recordDartError);
2326}
2427
2528class MyApp extends StatefulWidget {
29+ const MyApp ({Key ? key}) : super (key: key);
30+
2631 @override
27- _MyAppState createState () => _MyAppState ();
32+ State < MyApp > createState () => _MyAppState ();
2833}
2934
3035class _MyAppState extends State <MyApp > {
@@ -44,7 +49,7 @@ class _MyAppState extends State<MyApp> {
4449 Countly .isInitialized ().then ((bool isInitialized) {
4550 if (! isInitialized) {
4651 Countly .pushTokenType (Countly .messagingMode['TEST' ]! ); // Set messaging mode for push notifications
47-
52+
4853 var crashSegment = {'Key' : 'Value' };
4954 var userProperties = {'customProperty' : 'custom Value' , 'username' : 'USER_NAME' , 'email' : 'USER_EMAIL' };
5055
@@ -117,7 +122,7 @@ class _MyAppState extends State<MyApp> {
117122 static String SERVER_URL = 'https://master.count.ly' ;
118123
119124 // ignore: non_constant_identifier_names
120- static String APP_KEY = '58594c9a3f461ebc000761a68c2146659ef75ea0 ' ;
125+ static String APP_KEY = 'YOUR_API_KEY ' ;
121126
122127 void enableTemporaryIdMode () {
123128 Countly .changeDeviceId (Countly .deviceIDType['TemporaryDeviceID' ]! , false );
@@ -126,8 +131,8 @@ class _MyAppState extends State<MyApp> {
126131 bool isManualSession () {
127132 //
128133 if (! _enableManualSession) {
129- final snackBar = SnackBar (
130- content: const Text ("Set '_enableManualSession = true' in 'main.dart' to test Manual Session Handling" ),
134+ const snackBar = SnackBar (
135+ content: Text ("Set '_enableManualSession = true' in 'main.dart' to test Manual Session Handling" ),
131136 );
132137 _messangerKey.currentState! .showSnackBar (snackBar);
133138 }
@@ -184,21 +189,21 @@ class _MyAppState extends State<MyApp> {
184189
185190 void endEventBasic () {
186191 Countly .startEvent ('Timed Event' );
187- Timer (Duration (seconds: 5 ), () {
192+ Timer (const Duration (seconds: 5 ), () {
188193 Countly .endEvent ({'key' : 'Timed Event' });
189194 });
190195 }
191196
192197 void endEventWithSum () {
193198 Countly .startEvent ('Timed Event With Sum' );
194- Timer (Duration (seconds: 5 ), () {
199+ Timer (const Duration (seconds: 5 ), () {
195200 Countly .endEvent ({'key' : 'Timed Event With Sum' , 'sum' : '0.99' });
196201 });
197202 }
198203
199204 void endEventWithSegment () {
200205 Countly .startEvent ('Timed Event With Segment' );
201- Timer (Duration (seconds: 5 ), () {
206+ Timer (const Duration (seconds: 5 ), () {
202207 var event = {
203208 'key' : 'Timed Event With Segment' ,
204209 'count' : 1 ,
@@ -210,7 +215,7 @@ class _MyAppState extends State<MyApp> {
210215
211216 void endEventWithSumSegment () {
212217 Countly .startEvent ('Timed Event With Segment, Sum and Count' );
213- Timer (Duration (seconds: 5 ), () {
218+ Timer (const Duration (seconds: 5 ), () {
214219 var event = {'key' : 'Timed Event With Segment, Sum and Count' , 'count' : 1 , 'sum' : '0.99' };
215220 event['segmentation' ] = {'Country' : 'Turkey' , 'Age' : '28' };
216221 Countly .endEvent (event);
@@ -404,16 +409,16 @@ class _MyAppState extends State<MyApp> {
404409 Countly .askForNotificationPermission ();
405410 }
406411
407- void _showDialog (String alterText ) {
412+ void _showDialog (String alertText ) {
408413 showDialog (
409414 context: navigatorKey.currentContext! ,
410415 builder: (BuildContext context) {
411416 return AlertDialog (
412- title: Text ('Alert!!' ),
413- content: Text (alterText ),
417+ title: const Text ('Alert!!' ),
418+ content: Text (alertText ),
414419 actions: < Widget > [
415420 ElevatedButton (
416- child: Text ('OK' ),
421+ child: const Text ('OK' ),
417422 onPressed: () {
418423 Navigator .of (navigatorKey.currentContext! ).pop ();
419424 },
@@ -433,6 +438,7 @@ class _MyAppState extends State<MyApp> {
433438 });
434439 });
435440 }
441+
436442 void eventForGoal_1 () {
437443 var event = {'key' : 'eventForGoal_1' , 'count' : 1 };
438444 Countly .recordEvent (event);
@@ -497,7 +503,7 @@ class _MyAppState extends State<MyApp> {
497503 });
498504 }
499505
500- void getDeviceIDType () async {
506+ Future < void > getDeviceIDType () async {
501507 DeviceIdType ? deviceIdType = await Countly .getDeviceIDType ();
502508 if (deviceIdType != null ) {
503509 setState (() {
@@ -516,7 +522,7 @@ class _MyAppState extends State<MyApp> {
516522
517523 void addCrashLog () {
518524 Countly .addCrashLog ('User Performed Step A' );
519- Timer (Duration (seconds: 5 ), () {
525+ Timer (const Duration (seconds: 5 ), () {
520526 Countly .logException ('one.js \n two.js \n three.js' , true , {'_facebook_version' : '0.0.1' });
521527 });
522528 }
@@ -587,7 +593,7 @@ class _MyAppState extends State<MyApp> {
587593 });
588594 }
589595
590- void showFeedbackWidget () async {
596+ Future < void > showFeedbackWidget () async {
591597 FeedbackWidgetsResponse feedbackWidgetsResponse = await Countly .getAvailableFeedbackWidgets ();
592598 List <CountlyPresentableFeedback > widgets = feedbackWidgetsResponse.presentableFeedback;
593599 String ? error = feedbackWidgetsResponse.error;
@@ -598,14 +604,14 @@ class _MyAppState extends State<MyApp> {
598604
599605 if (widgets.isNotEmpty) {
600606 await Countly .presentFeedbackWidget (widgets.first, 'Close' , widgetShown: () {
601- print ('showFeedbackWidget widgetShown ' );
607+ print ('showFeedbackWidget widget shown ' );
602608 }, widgetClosed: () {
603- print ('showFeedbackWidget widgetClosed ' );
609+ print ('showFeedbackWidget widget closed ' );
604610 });
605611 }
606612 }
607613
608- void showSurvey () async {
614+ Future < void > showSurvey () async {
609615 FeedbackWidgetsResponse feedbackWidgetsResponse = await Countly .getAvailableFeedbackWidgets ();
610616 List <CountlyPresentableFeedback > widgets = feedbackWidgetsResponse.presentableFeedback;
611617 String ? error = feedbackWidgetsResponse.error;
@@ -622,7 +628,7 @@ class _MyAppState extends State<MyApp> {
622628 }
623629 }
624630
625- void showNPS () async {
631+ Future < void > showNPS () async {
626632 FeedbackWidgetsResponse feedbackWidgetsResponse = await Countly .getAvailableFeedbackWidgets ();
627633 List <CountlyPresentableFeedback > widgets = feedbackWidgetsResponse.presentableFeedback;
628634 String ? error = feedbackWidgetsResponse.error;
@@ -634,16 +640,16 @@ class _MyAppState extends State<MyApp> {
634640 for (CountlyPresentableFeedback widget in widgets) {
635641 if (widget.type == 'nps' ) {
636642 await Countly .presentFeedbackWidget (widget, 'Close' , widgetShown: () {
637- print ('NPS widgetShown ' );
643+ print ('NPS widget shown ' );
638644 }, widgetClosed: () {
639- print ('NPS widgetClosed ' );
645+ print ('NPS widget closed ' );
640646 });
641647 break ;
642648 }
643649 }
644650 }
645651
646- void reportSurveyManually () async {
652+ Future < void > reportSurveyManually () async {
647653 FeedbackWidgetsResponse feedbackWidgetsResponse = await Countly .getAvailableFeedbackWidgets ();
648654 List <CountlyPresentableFeedback > widgets = feedbackWidgetsResponse.presentableFeedback;
649655 String ? error = feedbackWidgetsResponse.error;
@@ -659,11 +665,11 @@ class _MyAppState extends State<MyApp> {
659665 }
660666 }
661667 if (chosenWidget != null ) {
662- reportSurvey (chosenWidget);
668+ unawaited ( reportSurvey (chosenWidget) );
663669 }
664670 }
665671
666- void reportSurvey (CountlyPresentableFeedback chosenWidget) async {
672+ Future < void > reportSurvey (CountlyPresentableFeedback chosenWidget) async {
667673 List result = await Countly .getFeedbackWidgetData (chosenWidget);
668674 String ? error = result[1 ];
669675 if (error == null ) {
@@ -679,7 +685,7 @@ class _MyAppState extends State<MyApp> {
679685 Map <dynamic , dynamic > question = questions[a];
680686 String wType = question['type' ];
681687 String questionId = question['id' ];
682- String answerKey = 'answ-' + questionId;
688+ String answerKey = 'answ-$ questionId ' ;
683689 switch (wType) {
684690 //multiple answer question
685691 case 'multi' :
@@ -718,7 +724,7 @@ class _MyAppState extends State<MyApp> {
718724 }
719725 }
720726
721- void reportNPSManually () async {
727+ Future < void > reportNPSManually () async {
722728 FeedbackWidgetsResponse feedbackWidgetsResponse = await Countly .getAvailableFeedbackWidgets ();
723729 List <CountlyPresentableFeedback > widgets = feedbackWidgetsResponse.presentableFeedback;
724730 String ? error = feedbackWidgetsResponse.error;
@@ -808,7 +814,7 @@ class _MyAppState extends State<MyApp> {
808814 child: SingleChildScrollView (
809815 child: Column (
810816 children: < Widget > [
811- Text (_deviceIdType, style: TextStyle (color: Colors .red), textAlign: TextAlign .center),
817+ Text (_deviceIdType, style: const TextStyle (color: Colors .red), textAlign: TextAlign .center),
812818 MyButton (text: 'Get Device Id Type' , color: 'green' , onPressed: getDeviceIDType),
813819 MyButton (text: 'Begin Session' , color: 'green' , onPressed: beginSession),
814820 MyButton (text: 'Update Session' , color: 'green' , onPressed: updateSession),
@@ -860,12 +866,10 @@ class _MyAppState extends State<MyApp> {
860866 MyButton (text: 'Remove Consent Push' , color: 'blue' , onPressed: removeConsentpush),
861867 MyButton (text: 'Remove Consent starRating' , color: 'blue' , onPressed: removeConsentstarRating),
862868 MyButton (text: 'Remove Consent Performance' , color: 'blue' , onPressed: removeConsentAPM),
863-
864- Text ("Section for A/B testing:" , style: TextStyle (color: Colors .green), textAlign: TextAlign .center),
869+ const Text ('Section for A/B testing:' , style: TextStyle (color: Colors .green), textAlign: TextAlign .center),
865870 MyButton (text: 'Get AB testing values' , color: 'green' , onPressed: getABTestingValues),
866871 MyButton (text: 'Record event for goal #1' , color: 'green' , onPressed: eventForGoal_1),
867872 MyButton (text: 'Record event for goal #2' , color: 'green' , onPressed: eventForGoal_2),
868-
869873 MyButton (text: 'Countly.remoteConfigUpdate' , color: 'purple' , onPressed: remoteConfigUpdate),
870874 MyButton (text: 'Countly.updateRemoteConfigForKeysOnly' , color: 'purple' , onPressed: updateRemoteConfigForKeysOnly),
871875 MyButton (text: 'Countly.updateRemoteConfigExceptKeys' , color: 'purple' , onPressed: updateRemoteConfigExceptKeys),
@@ -890,7 +894,7 @@ class _MyAppState extends State<MyApp> {
890894 MyButton (text: 'Open feedback modal' , color: 'orange' , onPressed: presentRatingWidget),
891895 TextField (
892896 controller: ratingIdController,
893- decoration: InputDecoration (
897+ decoration: const InputDecoration (
894898 border: OutlineInputBorder (),
895899 hintText: 'Enter a Rating ID' ,
896900 ),
@@ -914,20 +918,20 @@ class _MyAppState extends State<MyApp> {
914918}
915919
916920Map <String , Map <String , Color >> theColor = {
917- 'default' : {'button' : Color (0xffe0e0e0 ), 'text' : Color (0xff000000 )},
918- 'red' : {'button' : Color (0xffdb2828 ), 'text' : Color (0xff000000 )},
919- 'green' : {'button' : Colors .green, 'text' : Color (0xffffffff )},
920- 'teal' : {'button' : Color (0xff00b5ad ), 'text' : Color (0xff000000 )},
921- 'blue' : {'button' : Color (0xff00b5ad ), 'text' : Color (0xff000000 )},
922- 'primary' : {'button' : Color (0xff54c8ff ), 'text' : Color (0xff000000 )},
923- 'grey' : {'button' : Color (0xff767676 ), 'text' : Color (0xff000000 )},
924- 'brown' : {'button' : Color (0xffa5673f ), 'text' : Color (0xff000000 )},
925- 'purple' : {'button' : Color (0xffa333c8 ), 'text' : Color (0xff000000 )},
926- 'violet' : {'button' : Color (0xff6435c9 ), 'text' : Color (0xff000000 )},
927- 'yellow' : {'button' : Color (0xfffbbd08 ), 'text' : Color (0xffffffff )},
928- 'black' : {'button' : Color (0xff1b1c1d ), 'text' : Color (0xffffffff )},
929- 'olive' : {'button' : Color (0xffd9e778 ), 'text' : Color (0xff000000 )},
930- 'orange' : {'button' : Color (0xffff851b ), 'text' : Color (0xff000000 )}
921+ 'default' : {'button' : const Color (0xffe0e0e0 ), 'text' : const Color (0xff000000 )},
922+ 'red' : {'button' : const Color (0xffdb2828 ), 'text' : const Color (0xff000000 )},
923+ 'green' : {'button' : Colors .green, 'text' : const Color (0xffffffff )},
924+ 'teal' : {'button' : const Color (0xff00b5ad ), 'text' : const Color (0xff000000 )},
925+ 'blue' : {'button' : const Color (0xff00b5ad ), 'text' : const Color (0xff000000 )},
926+ 'primary' : {'button' : const Color (0xff54c8ff ), 'text' : const Color (0xff000000 )},
927+ 'grey' : {'button' : const Color (0xff767676 ), 'text' : const Color (0xff000000 )},
928+ 'brown' : {'button' : const Color (0xffa5673f ), 'text' : const Color (0xff000000 )},
929+ 'purple' : {'button' : const Color (0xffa333c8 ), 'text' : const Color (0xff000000 )},
930+ 'violet' : {'button' : const Color (0xff6435c9 ), 'text' : const Color (0xff000000 )},
931+ 'yellow' : {'button' : const Color (0xfffbbd08 ), 'text' : const Color (0xffffffff )},
932+ 'black' : {'button' : const Color (0xff1b1c1d ), 'text' : const Color (0xffffffff )},
933+ 'olive' : {'button' : const Color (0xffd9e778 ), 'text' : const Color (0xff000000 )},
934+ 'orange' : {'button' : const Color (0xffff851b ), 'text' : const Color (0xff000000 )}
931935};
932936
933937Map <String , Color >? getColor (color) {
@@ -961,25 +965,28 @@ Map<String, Color>? getColor(color) {
961965}
962966
963967class MyButton extends StatelessWidget {
964- String ? _text;
965- Color ? _button;
966- Color ? _textC;
967- void Function ()? _onPressed;
968-
969- MyButton ({String ? color, String ? text, void Function ()? onPressed}) {
970- _text = text! ;
971-
968+ final String _text;
969+ late final Color ? _button;
970+ late final Color ? _textC;
971+ final void Function ()? _onPressed;
972+
973+ MyButton ({
974+ required String text,
975+ String ? color,
976+ void Function ()? onPressed,
977+ Key ? key,
978+ }) : _text = text,
979+ _onPressed = onPressed,
980+ super (key: key) {
972981 Map <String , Color >? tColor;
973982 tColor = getColor (color);
974- tColor = tColor ?? = theColor['default' ];
983+ tColor ?? = theColor['default' ];
975984 _button = tColor? ['button' ];
976985 _textC = tColor? ['text' ];
977-
978- _onPressed = onPressed;
979986 }
980987
981988 @override
982989 Widget build (BuildContext context) {
983- return ElevatedButton (style: ElevatedButton .styleFrom (primary : _button, padding: EdgeInsets .all (10.0 ), minimumSize: Size (double .infinity, 36 )), onPressed: _onPressed, child: Text (_text! , style: TextStyle (color: _textC), textAlign: TextAlign .center));
990+ return ElevatedButton (style: ElevatedButton .styleFrom (backgroundColor : _button, padding: const EdgeInsets .all (10.0 ), minimumSize: const Size (double .infinity, 36 )), onPressed: _onPressed, child: Text (_text, style: TextStyle (color: _textC), textAlign: TextAlign .center));
984991 }
985992}
0 commit comments