Search code examples
flutterdartflutter-provider

A Provider was used after being disposed - Multiprovider


After I added the dependency of ProfileLogic to LocationLogic I get the following error when the app starts:

I/flutter (14674): A LocationLogic was used after being disposed.

I/flutter (14674): Once you have called dispose() on a LocationLogic, it can no longer be used.

These are my providers:

      providers: [
        ChangeNotifierProvider(builder: (_) => ConnectivityLogic()),
        ChangeNotifierProxyProvider<ConnectivityLogic, ProfileLogic>(
          builder: (context, connectivity, previousMessages) =>
              ProfileLogic(connectivity.isOnline),
          initialBuilder: (BuildContext context) => ProfileLogic(false),
        ),
        ChangeNotifierProxyProvider<ProfileLogic, LocationLogic>(
          builder: (context, profileLogic, previousMessages) =>
              LocationLogic(profileLogic.profile),
          initialBuilder: (BuildContext context) => LocationLogic(null),
        ),
        ChangeNotifierProvider(builder: (_) => SignUpModel()),
        ChangeNotifierProxyProvider<ConnectivityLogic, WorkLogic>(
          builder: (context, connectivity, previousMessages) =>
              WorkLogic(connectivity.isOnline),
          initialBuilder: (BuildContext context) => WorkLogic(false),
        ),
        ChangeNotifierProvider(builder: (_) => OrderLogic()),
      ]

The strange thing is that everything works properly, even with that error.


Solution

  • I think you disposed of a widget that holds those providers. Try to move desired providers higher in the tree. So if you have:

            MaterialApp(
              home: MultiProvider(
                providers: [...],
                child: child,
                )
            )
    

    Do something like:

            MultiProvider(
              providers: [...],
              child: MaterialApp(
                home: child,
              )
            )
    

    If this won't help you need to provide more context. eg. Whats widget tree like.