Skip to content

Using GetIt to provide a ChangeNotifier #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LeFrenchTouch opened this issue Apr 27, 2021 · 2 comments
Closed

Using GetIt to provide a ChangeNotifier #183

LeFrenchTouch opened this issue Apr 27, 2021 · 2 comments

Comments

@LeFrenchTouch
Copy link

LeFrenchTouch commented Apr 27, 2021

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:

  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();
    });
  }
}
@escamoteur
Copy link
Collaborator

Hi,

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

https://pub.dev/packages/get_it_mixin

I honestly don't know why listenable provider does not allow to use a ChangeNotifier that already has a listener

@LeFrenchTouch
Copy link
Author

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.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants