Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 430
Felipe Augusto Avatar asked Nov 17 '19 01:11

Felipe Augusto


People also ask

How do I get rid of Changenotifier in flutter?

dispose method Null safety Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed). This method should only be called by the object's owner.


1 Answers

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.

like image 164
Luke Urban Avatar answered Oct 09 '22 07:10

Luke Urban