在 flutter dart 中将 Future<int> 转换为 int
Convert Future<int> to int in flutter dart
我正在使用 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 变量中,以便在 onPressed
in FloatingActionButton
中使用它
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);
}
},
);
},
),
);
},
),
但我遇到了这个异常:
A value of type 'Future' can't be assigned to a variable of type 'int'.
使用 await
得到 Future
的响应
int number = await getNumberOfUsers();
int count = await getCount();
我通过为 OnPressed 添加异步解决了这个问题
onPressed: () async {...}
然后使用这一行代码
int count = await getCount();
感谢
您只需在调用 Future 之前设置关键字“await”即可:
你的工作:
int count = getCount();
正确的是:
int count = await getCount();
you need to add the "await" keyword before calling the function
int count = await getCount();
我正在使用 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 变量中,以便在 onPressed
in FloatingActionButton
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);
}
},
);
},
),
);
},
),
但我遇到了这个异常:
A value of type 'Future' can't be assigned to a variable of type 'int'.
使用 await
得到 Future
int number = await getNumberOfUsers();
int count = await getCount();
我通过为 OnPressed 添加异步解决了这个问题
onPressed: () async {...}
然后使用这一行代码
int count = await getCount();
感谢
您只需在调用 Future 之前设置关键字“await”即可:
你的工作:
int count = getCount();
正确的是:
int count = await getCount();
you need to add the "await" keyword before calling the function
int count = await getCount();