fluttercommunity / get_it

Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur
https://pub.dev/packages/get_it
MIT License
1.36k stars 149 forks source link

Null check operator used on a null value. Error thrown Instance of 'ErrorDescription'. #337

Closed klivin closed 1 year ago

klivin commented 1 year ago

Using get_it: ^7.6.0

I am getting several variants of the same bug in my production code and I'm wondering why get it would have a null instance in several different cases. I have 8 variants in 2 days. I cannot reproduce in developement mode of the app.

The line that keeps throwing is

      instance = instanceFactory.instance!;

inside your get_it_impl.dart

I am using this function to register singletons with get_it. I have not seen this problem surface but customers are complaining about seeing the error Null check operator used on a null value. Error thrown Instance of 'ErrorDescription'.

Please advise as to why this is happening and if there's a way to ensure the instance factory has the instance when accessed?

void registerService<T extends LoadableService>(
        T Function() factoryFunction,
        {bool lazy = false,
        List<Type>? dependsOn}) {
      var registerFcn = () async {
        final service = factoryFunction();
        await service.load();
        return service;
      };
      if (lazy) {
        locator.registerLazySingletonAsync<T>(registerFcn);
      } else {
        locator.registerSingletonAsync<T>(registerFcn, dependsOn: dependsOn);
      }
    }

Here are 3 of the 8 different stack traces.
VARIENT 1 0 ??? 0x0 _GetItImplementation.get + 437 (get_it_impl.dart:437) 1 ??? 0x0 _GetItImplementation.call + 461 (get_it_impl.dart:461) 2 ??? 0x0 _EffectsOfAddingState.build + 346 (EffectsOfAdding.dart:346) 3 ??? 0x0 StatefulElement.build + 5080 (framework.dart:5080)

VARIENT 2 0 ??? 0x0 _GetItImplementation.get + 437 (get_it_impl.dart:437) 1 ??? 0x0 _GetItImplementation.call + 461 (get_it_impl.dart:461) 2 ??? 0x0 Problem.titleString + 128 (Problem.dart:128) 3 ??? 0x0 _WaterTestServiceCellState._buildTestProblemRow + 178 (WaterTestServiceCell.dart:178) 4 ??? 0x0 _WaterTestServiceCellState._buildProblemList. + 325 (WaterTestServiceCell.dart:325)

VARIENT 3 0 ??? 0x0 _GetItImplementation.get + 437 (get_it_impl.dart:437) 1 ??? 0x0 _GetItImplementation.call + 461 (get_it_impl.dart:461) 2 ??? 0x0 _EffectsOfAddingState.build + 346 (EffectsOfAdding.dart:346) 3 ??? 0x0 StatefulElement.build + 5080 (framework.dart:5080)

escamoteur commented 1 year ago

Hey, honestly I'm not sure I know what you are trying to do in that piece of code. Also I would only use registerLazySingletonAsync if it is absolutely necessary.

The error signals that this happened indeed because you tried to access a not-ready instance.

please study https://github.com/fluttercommunity/get_it#synchronizing-asynchronous-initializations-of-singletons carefully or watch my talk on get_it https://www.youtube.com/watch?v=YJ52kSfSMyM&list=PL-BFoWYMGZ2TvwY0uf1fBJB1358utBlAz&index=10&t=14s&pp=gAQBiAQB

klivin commented 1 year ago

Thanks for getting back.

registerService uses a LoadableService mixin that all implement load() so I can register get it services like this and not have to keep calling load() on each one.

    registerService<SolutionContentService>(() => SolutionContentService());
    registerService<ProblemContentService>(() => ProblemContentService(),
        dependsOn: [SolutionContentService]);
    registerService<MaintenanceContentService>(
        () => MaintenanceContentService(),
        lazy: true);
    registerService<ProblemSeverityStrings>(() => ProblemSeverityStrings());
    registerService<TestStrips>(() => TestStrips());
    registerService<AchievementBadges>(() => AchievementBadges());
mixin LoadableService on Object {
  Future load();

  Future<dynamic> parseJsonFromAssets(String assetsPath) async {
    WidgetsFlutterBinding.ensureInitialized();

    return rootBundle
        .loadString(assetsPath)
        .then((jsonStr) => jsonDecode(jsonStr));
  }
}

A user found that reinstalling the app worked for them. This appears to happening for upgrade cases only so far. My hunch now is a bizarre exception thrown in my load() when parsing the json that would cause the singleton to never instantiate.

escamoteur commented 1 year ago

I'm not sure if I can be any help on this here. closing it for now