1

I've recently migrated to riverpod 14

I've change my provider so it now takes the generic types

class Counter extends StateNotifier<int> {
  Counter() : super(0);

  void increment() => state++;
}

final counterProvider = StateNotifierProvider<Counter,int>((ref) {
  return Counter();
});

Specifically... StateNotifierProvider<Counter,int>

But when I try to invoke the function increment

I get "The getter 'increment' isn't defined for the type 'int'"

class _LoginFormState extends State<LoginForm> {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () => context.read(counterProvider).increment,
      child: Text('increment'),
    );
  }
}

Not sure what I need to change in order to invoke increment

Any help welcome!

Thanks

aidanmack
  • 518
  • 1
  • 5
  • 16
  • 1
    just add .notifier like this `context.read(counterProvider.notifier).increment()` – Mohammed Alfateh May 24 '21 at 23:10
  • Yep! - This is the correct answer. It was in the migration documentation as well. I just missed it.. flips table. to obtain the StateNotifier, you should now read myProvider.notifier instead of just myProvider: Thanks very much for your help! – aidanmack May 25 '21 at 11:46
  • Does this answer your question? [The type 'StateNotifierProvider' is declared with 2 type parameters, but 1 type arguments were given](https://stackoverflow.com/questions/67233449/the-type-statenotifierprovider-is-declared-with-2-type-parameters-but-1-type) – Alex Hartford May 25 '21 at 17:15
  • Yes it does - I was trying to set it as the answer - but could not see how to. – aidanmack Jun 15 '21 at 17:40

0 Answers0