【Flutter】hooks_riverpod 1.0.0 (stable)にしたらuseProviderが使えなくなっていた

Flutter

結構な破壊的変更が入っているようです。

riverpod,flutter_riverpod,hooks_riverpodそれぞれでCHANGELOGに書かれている内容が微妙に違うので詳しくは使っているパッケージのものを参照しましょう。

それぞれ以下を参照してください。

  • riverpod
riverpod changelog | Dart package
A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
  • flutter_riverpod
flutter_riverpod changelog | Flutter package
A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
  • hooks_riverpod
hooks_riverpod changelog | Flutter package
A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.

useProviderの件

タイトルのuseProviderは個人的にめちゃくちゃ使っていたのですが、破壊的変更により消滅しました、、、

詳細はこちらに記載があります。

  • 以前までの書き方
class Example extends HookWidget {
  @override
  Widget build(BuildContext context) {
    useState(...);
    int count = useProvider(counterProvider);
    ...
  }
}
  • 1.0.0以降の書き方
class Example extends HookConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    useState(...);
    int count = ref.watch(counterProvider);
    ...
  }
}

HookWidgetを継承していたのが、HookConsumerWidgetを継承しなければいけなくなり、overrideするメソッドの引数もWidgetRefというのが追加になっています。

Providerを呼び出すのにuseProviderは使えなくなり、今後はref.watchで呼ぶ必要があるようです。

それ以外も、ConsumerWidgetを継承したときのbuildメソッドの引数がやはり変更になりこれもWidgetRefを受け取り、ref経由で呼び出すように変更になっています。

コメント

タイトルとURLをコピーしました