【发布时间】:2020-09-25 11:03:33
【问题描述】:
我有一个继承自 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 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;
但是,它不会更新。
【问题讨论】:
标签: flutter flutter-layout flutter-dependencies flutter-animation flutter-test