【发布时间】:2020-05-18 08:57:45
【问题描述】:
非常感谢您的帮助,因为我这几天一直在尝试解决这个问题,但仍然无法解决问题。
下面的所有代码都可以完美运行,直到最近,列表停止更新,直到我热重启它。
下面两个函数分别获取我的地图列表和我的笔记列表
Future<List<Map<String, dynamic>>> getNoteListMap() async {
Database db = await this.database;
var result = await db.query(noteTable);
return result;
}
Future<List<Note>> getNoteList() async {
var noteMapList = await getNoteListMap();
int count = noteMapList.length;
//list of notes, each note consist of their own independent variables
List<Note> noteList = new List<Note>();
for (int i = 0; i < count; i++) {
noteList.add(Note.fromMapObject(noteMapList[i]));
}
return noteList;
}
这是我的主要功能,它使用 FutureBuilder 创建一个列表。但是列表没有更新,只有在我每次热重启后才会更新。
FutureBuilder<List<Note>>(
future: _databaseHelper.getNoteList(),
builder: (BuildContext context, AsyncSnapshot<List<Note>> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
physics: TargetPlatform.iOS!=null? BouncingScrollPhysics() : ClampingScrollPhysics(),
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int position) {
Note note = snapshot.data[position];
return Card(
color: Colors.white,
elevation: 2.0,
child: new ListTile(
title: new Text(note.title?? ''),
subtitle: new Text(note.bodyText ?? ''),
onTap: () => _navigateToTaskByDatePage(date),
),
);
}
);
} else if (snapshot.hasError) {
return Container(
child: Center(
child: new Text('Error: ${snapshot.error}'),
),
);
} else {
return Container(
width: 0,
height: 0,
);
}
},
),
【问题讨论】:
-
您的代码似乎没有任何问题。由于我们无权访问您的数据库,因此很难帮助您进行调试。您是否尝试过对AsyncSnapshot 的ConnectionState 进行检查以查看发生了什么?
-
会调查一下,谢谢你的建议。
-
您是否尝试过使用
StreamBuilder而不是FutureBuilder? Here's 一个关于两者区别的问题和答案。 -
在你调用函数后尝试调用
setState()方法,它会改变应用状态。