A Provider was used after being disposed - Multiprovider
Asked Answered
L

2

8

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.

Lamelliform answered 17/11, 2019 at 1:5 Comment(1)
I am also facing same issue, did you find any solution?Garlic
K
6

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.

Koster answered 1/9, 2020 at 12:11 Comment(1)
Good guess, though I don't remember how I solved, now I use to create an architecture more based on streams than on state (notifyListeners()), the call of this method after some dispose would be my second guess.Lamelliform
C
0

When you switch screens, the provider gets disposed. Move the Provider one level up or above the Material app so that its available even after the widgets are rebuild when you switch screens.

Calen answered 18/2, 2024 at 12:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.