【问题标题】:How do i implement ChangeNotifier for a list for working with Provider?如何为与 Provider 合作的列表实施 ChangeNotifier?
【发布时间】:2020-05-29 14:34:21
【问题描述】:

我想为我的应用程序实现 Provider,在做了一些研究后,我发现我必须为我的 Data 类实现 ChangeNotifier 以在“天数”发生变化时更新 UI。

我见过有人在 setter 方法中编写 notifyListeners(),但总是针对单个属性,从不针对列表。什么是正确的实现?

谢谢!

这是我的班级和名单:

class Data with ChangeNotifier {
  List<Day> days = [
    Day(
      name: 'Monday',
      transactions: [
        Transaction(
          isExpense: true,
          name: 'Pizza',
          transactionType: TransactionType.food,
          amount: '120€',
        ),
        Transaction(
          isExpense: true,
          name: 'EDEKA',
          transactionType: TransactionType.payment,
          amount: '120€',
        ),
      ],
    ),
    Day(
      name: 'Tuesday',
      transactions: [
        Transaction(
          isExpense: true,
          name: 'Sushi',
          transactionType: TransactionType.food,
          amount: '120€',
        ),
        Transaction(
          isExpense: true,
          name: 'Lidl',
          transactionType: TransactionType.payment,
          amount: '120€',
        ),
        Transaction(
          isExpense: false,
          name: 'Einkommen',
          transactionType: TransactionType.payment,
          amount: '120€',
        ),
      ],
    ),
  ];
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    Todo App 是处理List&lt;Taks&gt; 的一个很好的例子
    可以参考https://dev.to/shakib609/create-a-todos-app-with-flutter-and-provider-jdh
    完整示例github代码https://github.com/shakib609/todos-flutter/tree/master/lib

    代码sn-p

    class Task {
      String title;
      bool completed;
    
      Task({@required this.title, this.completed = false});
    
      void toggleCompleted() {
        completed = !completed;
      }
    }
    
    class TodosModel extends ChangeNotifier {
      final List<Task> _tasks = [];
    
      UnmodifiableListView<Task> get allTasks => UnmodifiableListView(_tasks);
      UnmodifiableListView<Task> get incompleteTasks =>
          UnmodifiableListView(_tasks.where((todo) => !todo.completed));
      UnmodifiableListView<Task> get completedTasks =>
          UnmodifiableListView(_tasks.where((todo) => todo.completed));
    
      void addTodo(Task task) {
        _tasks.add(task);
        notifyListeners();
      }
    
      void toggleTodo(Task task) {
        final taskIndex = _tasks.indexOf(task);
        _tasks[taskIndex].toggleCompleted();
        notifyListeners();
      }
    
      void deleteTodo(Task task) {
        _tasks.remove(task);
        notifyListeners();
      }
    }
    

    工作演示

    【讨论】:

      【解决方案2】:

      制作2种添加和删除项目的方法。

      void add(Day day) {
        days.add(day);
        notifyListeners();
      }
      
      void remove(int index) {
        days.removeAt(index);
        notifyListeners();
      }
      

      【讨论】:

      • 好的,有道理。谢谢。所以如果我想编辑一天,我会同时使用,对吧?
      • 是的,或者如果您无法获取索引,您可以使用参数 Day day 创建一个方法并执行 'days.remove(day)'
      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2020-01-17
      • 2023-03-23
      • 2020-11-03
      • 2023-04-03
      • 2020-02-29
      相关资源
      最近更新 更多