1
0

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.

flutter/providerで状態管理を引き継ぐ方法

Posted at

個人の備忘録として

前提条件
flutter 1.20.2
Dart 2.9.1
provider: ^4.3.1(画面の状態管理のプラグイン)

投稿時点での推奨状態管理の方法であるproviderにて

Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => ProviderModel(),
child: Consumer(
builder: (context, model, child) {
},
));
}

このように状態を作りこれらを別画面でも共有したい場合、上記を再度行うと
もう一つ同じ名前のproviderができるだけで共有できません。
ではどうするか

Widget build(BuildContext context) {
final Model = Provider.of(context);
return ChangeNotifierProvider.value(
value: Model,
child: Consumer(builder: (context, model, child) {

final Model = Provider.of(context);
ここで引き継いでいます。<>の中の名称は一致させましょう。
また引き継ぐ側は決められた部分を上記のように変更することで引き継げます。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?