在 flutter dart 中将 Future<int> 转换为 int

Hud*_*uda 9 dart flutter sqflite

我正在使用sqflite,并且通过以下代码获取特定记录的行数:

  Future<int> getNumberOfUsers() async {
    Database db = await database;
    final count = Sqflite.firstIntValue(
        await db.rawQuery('SELECT COUNT(*) FROM Users'));
    return count;
  }
Run Code Online (Sandbox Code Playgroud)
  Future<int> getCount() async {
    DatabaseHelper helper = DatabaseHelper.instance;
    int counter = await helper.getNumberOfUsers();
    return counter;
  }
Run Code Online (Sandbox Code Playgroud)

我想将此函数的结果放入 int 变量中以在内部使用onPressedFloatingActionButton

int count = getCount();
int countParse = int.parse(getCount());
Run Code Online (Sandbox Code Playgroud)
    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);
                        }
                      },
                    );
                  },
                ),
              );
            },
          ),
Run Code Online (Sandbox Code Playgroud)

但我得到了这个例外:

“Future”类型的值不能分配给“int”类型的变量。

Hud*_*uda 10

我通过为 OnPressed 添加异步解决了这个问题

onPressed: () async {...}
Run Code Online (Sandbox Code Playgroud)

然后使用这行代码

int count = await getCount();
Run Code Online (Sandbox Code Playgroud)

谢谢