Hello, I am facing issue to fetch the feature flag...
# sdk-flutter
b
Hello, I am facing issue to fetch the feature flag in flutter app. Do anyone know how can I get the feature flag? I attached the SS of my GB feature which i am reading in the app using GB SDK.
r
Hi Yasir, can you share the code you're using in the Flutter app to fetch the feature?
b
@brief-honey-45610 here’s my code to fetch the features.
Copy code
class GrowthBookProvider with ProviderLoggy {
  const GrowthBookProvider();

  static const _apiKey = 'sdk-api-key';
  static const _hostURL = '<https://cdn.growthbook.io/>';

  static late final GrowthBookSDK _growthBook;

  Future<Experiments> init(String userId) async {
    _growthBook = await GBSDKBuilderApp(
      apiKey: _apiKey,
      hostURL: _hostURL,
      attributes: {
        'id': userId,
      },
      growthBookTrackingCallBack: (gbExperiment, gbExperimentResult) {},
    ).initialize();

    final featureFlags = <FeatureFlag, FeatureFlagResult<dynamic>>{
      for (final featureFlag in FeatureFlag.values)
        featureFlag: () {
          final result = _growthBook.feature(featureFlag.id);
          logInfo('Feature flag: ${featureFlag.id}, '
              'value: ${result.value}, source: ${result.source}');

          final experimentResult = (result.experiment != null &&
                  result.experimentResult != null)
              ? ExperimentResult(
                  experimentId: result.experiment!.key!,
                  variantId: result.experimentResult!.variationID!.toString(),
                )
              : null;

          return switch (featureFlag.type) {
            FeatureFlagType.bool => FeatureFlagResult<bool>(
                feature: featureFlag,
                value:
                    result.value as bool? ?? featureFlag.defaultValue as bool,
                experimentResult: experimentResult,
              ),
          };
        }(),
    };

    return Experiments(
      allFeatureFlags: featureFlags,
    );
  }
}
Copy code
enum FeatureFlag {
  wappShort('wapp_short', FeatureFlagType.bool, false),
  wappPick('wapp_pick', FeatureFlagType.bool, false),
  moreRecipes('wapp_paywall', FeatureFlagType.bool, false),
  ;

  const FeatureFlag(this.id, this.type, this.defaultValue);

  final String id;
  final FeatureFlagType type;
  final dynamic defaultValue;
}
@brief-honey-45610 My logs showing this -> INFO Global Loggy - Feature flag: wapp_short, value: null, source: GBFeatureSource.unknownFeature
b
@most-spoon-61816 or @cool-apple-97384 or @gifted-cat-24658 Would you be able to help Yasir?
m
Yes, we will help
👍 1