Convert Future<int> to int in flutter dart
Asked Answered
S

4

9

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'.

Shipentine answered 27/1, 2020 at 10:47 Comment(5)
It seems you already know how to use the await keyword. Can you explain why it would not be sufficient here?Russellrusset
Does this answer your question? How to get primitive value from Future object (Future<int>) in flutter?Russellrusset
Either use a FutureBuilder or use await keyword in order to get Future's value.Pas
@Russellrusset this line of code will cause exception int count = getCount(); I want to get the value as an integer and then check that it is not greater than 100Shipentine
I know what you want. But you have used the method to resolve a Future<T> to a T 3 times in the code you posted. Why is that not good enough now? Why can't you use it a fourth time?Russellrusset
S
10

I fixed this by adding async for OnPressed

onPressed: () async {...}

then use this line fo code

int count = await getCount();

thx

Shipentine answered 27/1, 2020 at 12:30 Comment(0)
S
3

All you need is to set the keyword "await" before calling the Future:

what you do:

int count = getCount(); 

what is correct:

int count = await getCount();
Sinhalese answered 7/2, 2021 at 17:59 Comment(0)
J
2

use await to get response of Future

int number = await getNumberOfUsers();

int count = await getCount();
Jedthus answered 27/1, 2020 at 12:17 Comment(0)
I
1
you need to add the "await" keyword before calling the function

int count = await getCount();

Immunochemistry answered 2/6, 2021 at 13:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.