4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

riverpodでProviderScopeを使った時のトラブルシューティング

Last updated at Posted at 2020-10-25

ProviderScopeをoverrideする際に、Cannot override providers on a non-root ProviderContainer/ProviderScope とエラーが出た時は、以下のようにScopedProviderを使うと解決します。

https://github.com/rrousselGit/river_pod/pull/49#issuecomment-658603800 より引用

final itemIndex = ScopedProvider<int>(null);
...
ListView(
  children: [
    ProviderScope(
      overrides: [
        productId.overrideAs(0)
      ],
      child: Item(),
    ),
    ProviderScope(
      overrides: [
        productId.overrideAs(1)
      ],
      child: Item(),
    ),
  ],
),
...
// inside Item
Widget build(context) {
  final itemId = useProvider(productId);
}

ScopedProviderははじめて知ったのですが、Scopedな(一部分で使われる、動的に作られる)Providerということがわかりやすくなって良いアップデートだと思います。

riverpodはアップデートが頻繁なので時々困ることもありますね・・

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?