flutterdartsqflite

Convert Future<int> to int in flutter dart


I am using sqflite and I am getting rows count of specific record via below code:

  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;
  }

I want to get the result of this function into int variable to use it inside 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);
                        }
                      },
                    );
                  },
                ),
              );
            },
          ),

but I am getting this exception:

A value of type 'Future' can't be assigned to a variable of type 'int'.


Solution

  • I fixed this by adding async for OnPressed

    onPressed: () async {...}
    

    then use this line fo code

    int count = await getCount();
    

    thx