将Future 转换为flart dart中的int

问题描述 投票:0回答:1

我正在使用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中的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);
                        }
                      },
                    );
                  },
                ),
              );
            },
          ),

但是我遇到了这个例外:

不能将类型为'Future'的值分配给类型为'int'的变量。

flutter dart sqflite
1个回答
-1
投票

尝试一下

替换int count = getCount();此代码

getCount().then((onValue){
int count = onValue;
}); 

通过此代码上方可获得未来值

您的完整代码在这里。

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(
                          () {
                            //--------------------------------------------
                            //You will get the count value here

                             getCount().then((onValue){
                              int count = onValue;
                                });

                            //--------------------------------------------
                            if (newTitle != null && newTitle.trim().isNotEmpty) {
                              _save(newTitle);
                            }
                          },
                        );
                      },
                    ),
                  );
                },
              ),
© www.soinside.com 2019 - 2024. All rights reserved.