简体   繁体   English

flutter_riverpod 如何监听列表

[英]How flutter_riverpod listens to a list

I want to use flutter_riverpod to watch a list of data.我想使用 flutter_riverpod 来观看数据列表。 But when I change the data content in the list, the widget is not refreshed.但是当我更改列表中的数据内容时,小部件不会刷新。

flutter_riverpod: ^1.0.0-dev.7 flutter_riverpod: ^1.0.0-dev.7

Part of the code is as follows:部分代码如下:

final personProvider = ChangeNotifierProvider((ref) => PersonProvider());

class PersonProvider extends ChangeNotifier {
  List<Person> personList;

  PersonProvider() {
    personList = [
      Person(name: "jack", age: 20),
      Person(name: "rose", age: 18),
    ];
  }
  
  void changeData() {
    // change person
    //personList[0].name = "new jack";
    // add person
    personList.add(Person(name: "lili", age: 25));
    notifyListeners();
  }
}

The build method of the page is as follows:页面的构建方法如下:

@override
  Widget build(BuildContext context) {
    // watch List
    List<Person> list = ref.watch(personProvider.select((value) => value.personList));
    return Scaffold(
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.change_circle),
          onPressed: () {
            // modify the data of list 
            ref.read(personProvider).changeData();
          },
        ),
        appBar: AppBar(),
        body: Column(
          children: list
              .map((person) => ListTile(
                    title: Text(person.name),
                    subtitle: Text("${person.age}"),
                  ))
              .toList(),
        ));
  }

Code link 代码链接

How to use flutter_riverpod to watch the data changes in the list, thanks!!!如何使用flutter_riverpod查看列表中的数据变化,谢谢!!!

Taking some liberty to change your code around just a bit;稍微改变一下你的代码; when working with Riverpod, I'll use StateNotifier over ChangeNotifier any day of the week.在使用 Riverpod 时,我将在一周中的任何一天使用StateNotifier不是ChangeNotifier It allows, in my opinion, for more structured logic.在我看来,它允许更结构化的逻辑。 Your code would look like this.你的代码看起来像这样。

class PersonNotifier extends StateNotifier<List<Person>> {
  PersonNotifier()
      : super([
          Person(name: "jack", age: 20),
          Person(name: "rose", age: 18),
        ]);

  void changeData(Person person) {
    state = [...state, person];
  }
}

final StateNotifierProvider<PersonNotifier, List<Person>> personProvider =
    StateNotifierProvider((ref) => PersonNotifier());

And you can then listen for your state in your code like this.然后你可以像这样在你的代码中监听你的状态。

List<Person> list = ref.watch(personProvider);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 flutter_riverpod 更新 StateProvider - update StateProvider with flutter_riverpod Flutter中的flutter_riverpod如何更方便的将ConsumerWidget转换为ConsumerStatefulWidget? - How does flutter_riverpod in Flutter convert ConsumerWidget to ConsumerStatefulWidget more conveniently? 如何在 TextFormFields 上使用 Flutter_Riverpod 和 TextEditingControllers 避免 markNeedsBuilder() 错误? - How do avoid markNeedsBuilder() error using Flutter_Riverpod and TextEditingControllers on TextFormFields? 为什么初始化期间flutter_riverpod出现state更改错误 - Why state change error occurs on flutter_riverpod during initialization Flutter_riverpod 安装时出现问题 - Flutter_riverpod giving me problems while installing flutter_riverpod - 在 `context` 可用时访问 `Ref` - flutter_riverpod - Accessing a `Ref` when `context` is available URI 的目标不存在:&#39;package:flutter_riverpod/flutter_riverpod.dart&#39; - Target of URI doesn't exist: 'package:flutter_riverpod/flutter_riverpod.dart' 当通知程序尝试在flutter_riverpod中更新其state错误时抛出异常 - Threw an exception when the notifier tried to update its state error in flutter_riverpod flutter_riverpod 1.0.0 阻止应用程序在调试模式下在 Chrome 中运行 - flutter_riverpod 1.0.0 prevents app from running in Chrome in debug mode Flutter:如何将 Riverpod 与 SharedPreference 和 List 结合使用<string>多页变量?</string> - Flutter : How to use Riverpod with SharedPreference and List<String> Variable in multipage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM