0
1

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 1 year has passed since last update.

# Flutter/Riverpodで値が更新されないときの原因(特にリスト)

Posted at

処方箋

特にリストだけが更新されない場合は以下のようにしてないか確認してください

ダメな例↓

main.dart
ref.read(listProvider.notifier).state = list; //NG

OKな例↓

main.dart
ref.read(listProvider.notifier).state = [...list]; //OK

原因

riverpodはリストの中身の変更を監視していません。

以下参照

// Todo の追加
  void addTodo(Todo todo) {
    // ステート自体もイミュータブルなため、`state.add(todo)`
    // のような操作はできません。
    // 代わりに、既存 Todo と新規 Todo を含む新しいリストを作成します。
    // Dart のスプレッド演算子を使うと便利ですよ!
    state = [...state, todo];
    // `notifyListeners` などのメソッドを呼ぶ必要はありません。
    // `state =` により必要なときに UI側 に通知が届き、ウィジェットが更新されます。
  }
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?