11import 'dart:async' ;
22import 'dart:convert' ;
3+
34import 'package:collection/collection.dart' show IterableExtension;
5+ import 'package:dio/dio.dart' ;
46import 'package:flutter_client_sse/constants/sse_request_type_enum.dart' ;
57import 'package:flutter_client_sse/flutter_client_sse.dart' ;
68import 'package:rxdart/subjects.dart' ;
7- import 'package:dio/dio.dart' ;
89
910import '../flagsmith.dart' ;
1011
@@ -35,10 +36,10 @@ class FlagsmithClient {
3536 late StorageProvider storageProvider;
3637 CoreStorage ? storage;
3738 late Dio _api;
38- final Set < Flag > _flags = {};
39+ final Map < String , Flag > _flags = {};
3940 final List <Flag > seeds;
4041
41- Set < Flag > get cachedFlags => _flags;
42+ Map < String , Flag > get cachedFlags => _flags;
4243
4344 //A map of flag names to the amount of times they have been evaluated in the last 10 seconds
4445 final Map <String , int > flagAnalytics = {};
@@ -293,9 +294,8 @@ class FlagsmithClient {
293294 throw FlagsmithConfigException (Exception ('caches are NOT enabled!' ));
294295 }
295296
296- var feature = _flags.firstWhereOrNull ((element) =>
297- element.feature.name == featureName && element.enabled == true );
298- return feature != null ;
297+ final feature = cachedFlags[featureName];
298+ return feature? .enabled == true ;
299299 }
300300
301301 /// Check if Feature flag exist and is enabled
@@ -339,8 +339,7 @@ class FlagsmithClient {
339339 log ('Exception: caches are NOT enabled!' );
340340 throw FlagsmithConfigException (Exception ('caches are NOT enabled!' ));
341341 }
342- var feature = cachedFlags
343- .firstWhereOrNull ((element) => element.feature.name == featureId);
342+ final feature = cachedFlags[featureId];
344343 _incrementFlagAnalytics (feature);
345344 return feature? .stateValue;
346345 }
@@ -360,7 +359,7 @@ class FlagsmithClient {
360359 Future <bool > removeFeatureFlag (String featureName) async {
361360 var result = await storageProvider.delete (featureName);
362361 if (config.caches) {
363- _flags.removeWhere ((element) => element.feature.name == featureName);
362+ _flags.remove ( featureName);
364363 }
365364 return result;
366365 }
@@ -542,11 +541,11 @@ class FlagsmithClient {
542541
543542 /// Internal updadte caches from list of featurs
544543 void _updateCaches ({List <Flag > list = const < Flag > []}) {
545- if (config.caches) {
546- _flags
547- ..clear ()
548- ..addAll (list.toSet ());
544+ if (! config.caches) {
545+ return ;
549546 }
547+ _flags.clear ();
548+ _flags.addEntries (list.map ((flag) => MapEntry (flag.feature.name, flag)));
550549 }
551550
552551 /// clear all data from storage
@@ -576,11 +575,10 @@ class FlagsmithClient {
576575 ///
577576 Future <bool > testToggle (String featureName) async {
578577 final result = await storageProvider.togggleFeature (featureName);
579- final value = await storageProvider.read (featureName);
580578 if (config.caches) {
581- _flags. removeWhere ((element) => element.feature.name == featureName);
579+ final value = await storageProvider. read ( featureName);
582580 if (value != null ) {
583- _flags. add ( value) ;
581+ _flags[featureName] = value;
584582 }
585583 }
586584 return result;
0 commit comments