ayalma / feature_discovery

Feature discavery based on material design
MIT License
152 stars 102 forks source link

hasPreviouslyCompleted() `Null check operator used on a null value` #81

Open flukejones opened 3 years ago

flukejones commented 3 years ago

Pretty simple to reproduce:

  @override
  void initState() {
    FeatureDiscovery.hasPreviouslyCompleted(context, 'featureHelpMachineListOptions')
    .then((value) {
      if (!value) {
        SchedulerBinding.instance?.addPostFrameCallback((Duration duration) {
          doingFeature = true;
          FeatureDiscovery.discoverFeatures(context, _helpFeatures);
        });
      }
    });

    super.initState();
  }

If the feature has never run before, then the following occurs:

  [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
#0      SharedPreferencesProvider.completedSteps (package:feature_discovery/src/foundation/persistence_provider.dart:50:23)
<asynchronous suspension>
#1      Bloc.hasPreviouslyCompleted (package:feature_discovery/src/foundation/bloc.dart:157:24)
<asynchronous suspension>
flukejones commented 3 years ago

I fixed like this:

  @override
  Future<Set<String?>> completedSteps(Iterable<String?>? featuresIds) async {
    final prefs = await SharedPreferences.getInstance();
    if (featuresIds == null) {
      return Set();
    }
    return featuresIds
        .where((featureId) =>
            prefs.getBool(_normalizeFeatureId(featureId)) == true)
        .toSet();
  }