我正在使用sqflite,通过以下代码获取特定记录的行数:
Future<int> getNumberOfUsers() async {
Database db = await database;
final count = Sqflite.firstIntValue(
await db.rawQuery('SELECT COUNT(*) FROM Users'));
return count;
}
Future<int> getCount() async {
DatabaseHelper helper = DatabaseHelper.instance;
int counter = await helper.getNumberOfUsers();
return counter;
}
我希望将这个函数的结果存入int变量中,以便在FloatingActionButton
中的onPressed
函数中使用。
int count = getCount();
int countParse = int.parse(getCount());
return Stack(
children: <Widget>[
Image.asset(
kBackgroundImage,
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
Scaffold(
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
child: Icon(
Icons.add,
color: kButtonBorderColor,
size: 30.0,
),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => AddScreen(
(String newTitle) {
setState(
() {
//--------------------------------------------
//I want to get the value here
int count = getCount();
int countParse = int.parse(getCount());
//--------------------------------------------
if (newTitle != null && newTitle.trim().isNotEmpty) {
_save(newTitle);
}
},
);
},
),
);
},
),
但我遇到了这个异常:
无法将“Future”类型的值分配给“int”类型的变量。
await
关键字了。你能解释一下为什么在这里它不够用吗? - nvoigtFutureBuilder
,要么使用await
关键字来获取Future
的值。 - Augustin RFuture<T>
解析为T
的方法,为什么现在这还不够好呢?为什么不能再用一次呢? - nvoigt