gpt4 book ai didi

flutter - 如何正确使用FutureBuilder?

转载 作者:行者123 更新时间:2023-12-03 04:20:52 26 4
gpt4 key购买 nike

我有一个ListView,我要在其中显示通过dbhelper从我的应用程序的SQFLite DB中读取的 bool(boolean) 值。
由于读写是异步的,因此我需要一些时间来了解FutureBuilder
DB-Helper方法是:

Future<Map<String, dynamic>> getItem(String tableName, String id) async {
Database db = await this.db;
var result = await db.rawQuery(
"SELECT * FROM $tableName WHERE $colId = $id ORDER BY $colCategory DESC, $colName DESC");
Map<String, dynamic> singleMap = result[0];
return singleMap;
}

我的提取方法是:
 Future<bool> isObtainedDB(Settings settings) async {
var map = await _dbHelper.getItem(_dbHelper.tblSavedata, id);
//var map = Map<String, dynamic>();
bool obtained = map[_dbHelper.colObtained];
return obtained;
}
我被困在试图将构建器包装在Checkbox周围的地方,以证明概念:
FutureBuilder(
future: isObtainedDB(settings),
builder: (context, snapshot) {
return CheckboxListTile(
title: Text(localization.obtained),
controlAffinity: ListTileControlAffinity.leading,
value: isObtainedDB(settings),
onChanged: (value) {
setObtained(settings, value);
});}),
现在如何在CheckBoxListTile中使用isObtainedDB中的 bool(boolean) 值?

最佳答案

将来的值将保存在builder函数中的snapshot.data中,因此,如果isObtainedDB(settings)返回了 bool(boolean) 值,则需要像这样访问它:snapshot.data(保存异步函数的值)

FutureBuilder(
future: isObtainedDB(settings),
builder: (context, snapshot) {
return CheckboxListTile(
title: Text(localization.obtained),
controlAffinity: ListTileControlAffinity.leading,
value: snapshot.data, //true or false
onChanged: (value) {
setObtained(settings, value);
}
);
}
)

关于flutter - 如何正确使用FutureBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62680819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com