You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having trouble using GetIt to provide a service that is a ChangeNotifier.
The issue seems to be that when the Consumer class is instantiated, ListenableProvider's code checks that if the listenable object being provided is a ChangeNotifier, it has no listeners attached.
Since I have a listener attached to that ChangeNotifier, the ListenableProvider's assert fails (listenable_provider.dart line 43).
Is it not possible to use GetIt to provide objects that are ChangeNotifiers and have listeners attached to those objects?
Any help will be greatly appreciated.
Cheers
Sample code:
GetIt.instance.registerSingletonAsync<A>(() async {
var a = A();
await a.init();
return a;
});
GetIt.instance.registerSingletonWithDependencies<B>(() {
final b = B();
b.init();
return b;
}, dependsOn: [A]);
await locator.allReady();
class A extends ChangeNotifier {
Future<void> init() async {}
}
class B {
final _a = GetIt.instance<A>();
void init() {
GetIt.instance
.isReady<A>()
.then((_) => _a.addListener(_updateData));
}
}
class Page extends StatefulWidget {}
class _PageState extends State<Page> {
@override
Widget build(BuildContext contex) {
return Consumer<A>(builder: (context, a, child) { <= This results in the ListenableProvider's assert failing
return Container();
});
}
}
The text was updated successfully, but these errors were encountered:
I think you are mixing up some things here. You can indeed use GetIt to provide the ChangeNotifier.
The problem here lies in provider and not in get_it.
I recommend use my get_it_mixing instead of provider because it works seamlessly together with get_it
Thanks for the fast response Patrick, and yes the issue is definitely happening in ListenableProvider and not GetIt, I just wasn't sure if there was a way to avoid the problem that I wasn't aware of (i.e. others have ran into this before, this is the usage pattern for ChangeNotifiers and GetIt ...).
I will definitely look at the GetIt Mixin, cheers!
Also worth noting, the issue doesn't happen in release code since it's an assert.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I'm having trouble using GetIt to provide a service that is a ChangeNotifier.
The issue seems to be that when the Consumer class is instantiated, ListenableProvider's code checks that if the listenable object being provided is a ChangeNotifier, it has no listeners attached.
Since I have a listener attached to that ChangeNotifier, the ListenableProvider's assert fails (listenable_provider.dart line 43).
Is it not possible to use GetIt to provide objects that are ChangeNotifiers and have listeners attached to those objects?
Any help will be greatly appreciated.
Cheers
Sample code:
The text was updated successfully, but these errors were encountered: