Search code examples
flutterdartriverpodflutter-riverpod

Changes in Flutter Riverpod 2.0


Recently, Flutter Riverpod 2.0 was released. I'm not sure what has changed with Flutter Riverpod 2.0. I hope someone can explain these changes to me.

If anyone can tell me what the changes are, I'd really appreciate it. Thank you in advance!


Solution

    1. If you used to pass parameter ref.read, now you should pass parameter ref => removed typedef Reader (as I understand, related to complication of testing), for example:
    final myClassProvider1 = Provider<MyClass>((ref) {
      return MyClass(ref);
    });
    
    // or using tear-off
    
    final myClassProvider2 = Provider<MyClass>(MyClass.new);
    
    class MyClass {
      MyClass(this.ref);
    
      [-] final Reader reader;
      [+] final Ref ref;
    }
    

    For a quick solution, you can create your own Reader. However, this is not recommended, BUT will work as a quick fix.

    typedef Reader = T Function<T>(ProviderBase<T> provider);
    
    1. In the meantime, you can use overrideWithProvider instead overrideWithValue.

    And check with riverpod/changelog. Also, you can wait for the official article from Remi Rousselet:

    An article is in progress and will be linked here once available.