更改通知程序中的对象列表不更新
List of Objects In Change Notifier don't update
我有一个继承自 ChangeNotifier 的对象列表。我删除了类型只是为了简化内容。
class TaskListsState with ChangeNotifier {
List _lists = List();
_currentList;
List get lists => _lists;
get currentList => _currentList;
set lists(List newValue) {
this._lists = newValue;
notifyListeners();
}
set currentList(newValue) {
this._currentList = newValue;
notifyListeners();
}
TaskListsState() {
this._lists = [
ToDoListState(),
ToCreateListState(),
ToDecideListState(),
BalancedMeListState()
];
this._currentList = this._lists[0];
}
}
所有对象都继承自一个基类 class,但通常,它们看起来是这样的
class ToCreateListState with ChangeNotifier implements TaskListState {
String title = "TO CREATE LIST";
String alias = "tocreate";
bool _loading = false;
List<TaskState> _tasks;
bool get loading => _loading;
List<TaskState> get tasks => _tasks;
set tasks(List<TaskState> newValue) {
this._tasks = newValue;
notifyListeners();
}
set loading(bool newValue) {
_loading = newValue;
notifyListeners();
}
ToCreateListState();
removeTask(TaskState task) {
this._tasks.remove(task);
notifyListeners();
}
}
现在,当我更新其中一个列表对象时,它们不会在 UI 中更新。请问我该如何解决这个问题?
目前,这就是我在视图中使用它的方式:
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => TaskListsState(),
),
],
child: MaterialApp(
title: '',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'Montserrat',
visualDensity: VisualDensity.adaptivePlatformDensity,
),
initialRoute: defaultHome,
onGenerateRoute: onGenerateRoute,
),
);
然后当我想在我的屏幕上使用其中一项时,我会做 Provider.of<TaskListsState>(context).lists[index].whateverValue =. anotherValue;
但是,它没有更新。
当您 运行 时 Provider.of<TaskListsState>(context).lists[index].whateverValue =. anotherValue;
它不会调用 notifyListeners()
因为它不会更新 lists
本身。
您可以简单地在 TaskListsState
中创建一个方法来为您进行修改,然后调用 notifyListeners()
class TaskListsState with ChangeNotifier {
...
void updateWhateverValue(int index, dynamic value) {
lists[index].whateverValue = value;
notifyListeners();
}
...
}
您应该将 dynamic
替换为您拥有的对象。
这是另一种解释:
假设我们有这个 class
class Products with ChangeNotifier
{
List<String> prods =new List() :
List<String> getProds => prods;
Void addprod()
{
prods.add('chocolate');
prods.add('beer');
notifyListeners();
}
Products()
{
addprod();
}
}
所以我们有 class 个产品,其中已经有两个产品。
删除产品的情况,如果我们调用
Provider.of<Products>(context)().products.remove('beer')
Beer 将被删除,但所有其他消费者将不会收到通知,因为删除是预定义的功能,并且在产品 class 中不存在作为通知程序。
确保每次调用函数时,它实际上是在更新数据模型中的某些内容class,以便您可以注意到更改。
希望澄清你的愿景。
我有一个继承自 ChangeNotifier 的对象列表。我删除了类型只是为了简化内容。
class TaskListsState with ChangeNotifier {
List _lists = List();
_currentList;
List get lists => _lists;
get currentList => _currentList;
set lists(List newValue) {
this._lists = newValue;
notifyListeners();
}
set currentList(newValue) {
this._currentList = newValue;
notifyListeners();
}
TaskListsState() {
this._lists = [
ToDoListState(),
ToCreateListState(),
ToDecideListState(),
BalancedMeListState()
];
this._currentList = this._lists[0];
}
}
所有对象都继承自一个基类 class,但通常,它们看起来是这样的
class ToCreateListState with ChangeNotifier implements TaskListState {
String title = "TO CREATE LIST";
String alias = "tocreate";
bool _loading = false;
List<TaskState> _tasks;
bool get loading => _loading;
List<TaskState> get tasks => _tasks;
set tasks(List<TaskState> newValue) {
this._tasks = newValue;
notifyListeners();
}
set loading(bool newValue) {
_loading = newValue;
notifyListeners();
}
ToCreateListState();
removeTask(TaskState task) {
this._tasks.remove(task);
notifyListeners();
}
}
现在,当我更新其中一个列表对象时,它们不会在 UI 中更新。请问我该如何解决这个问题?
目前,这就是我在视图中使用它的方式:
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => TaskListsState(),
),
],
child: MaterialApp(
title: '',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'Montserrat',
visualDensity: VisualDensity.adaptivePlatformDensity,
),
initialRoute: defaultHome,
onGenerateRoute: onGenerateRoute,
),
);
然后当我想在我的屏幕上使用其中一项时,我会做 Provider.of<TaskListsState>(context).lists[index].whateverValue =. anotherValue;
但是,它没有更新。
当您 运行 时 Provider.of<TaskListsState>(context).lists[index].whateverValue =. anotherValue;
它不会调用 notifyListeners()
因为它不会更新 lists
本身。
您可以简单地在 TaskListsState
中创建一个方法来为您进行修改,然后调用 notifyListeners()
class TaskListsState with ChangeNotifier {
...
void updateWhateverValue(int index, dynamic value) {
lists[index].whateverValue = value;
notifyListeners();
}
...
}
您应该将 dynamic
替换为您拥有的对象。
这是另一种解释: 假设我们有这个 class
class Products with ChangeNotifier
{
List<String> prods =new List() :
List<String> getProds => prods;
Void addprod()
{
prods.add('chocolate');
prods.add('beer');
notifyListeners();
}
Products()
{
addprod();
}
}
所以我们有 class 个产品,其中已经有两个产品。
删除产品的情况,如果我们调用
Provider.of<Products>(context)().products.remove('beer')
Beer 将被删除,但所有其他消费者将不会收到通知,因为删除是预定义的功能,并且在产品 class 中不存在作为通知程序。
确保每次调用函数时,它实际上是在更新数据模型中的某些内容class,以便您可以注意到更改。
希望澄清你的愿景。