4

I've looked everywhere and found no solution. I'm wanting to return the result count from a query, yet I'm having troubles. Keep getting "Instance of 'Future" instead of String

I'm using the Singleton pattern.

  • DB method (database.dart):
    Future<int> getUnCompletedTaskCount () async {
    final db = await database;
    var result = await db.rawQuery("SELECT * FROM Tasks WHERE isComplete = 0");
    int count = Sqflite.firstIntValue(result);
    return count;
  }
  • My method in main.dart:
    getResultCount () async {
    final value = await DBProvider.db.getUnCompletedTaskCount();
    return value;
  }
  • Calling the method by:
    getResultCount().toString()
4
  • You are using 'async' on your function so You can use FutureBuilder to fetch data from database, api ,... Commented Dec 6, 2019 at 0:09
  • Thanks for the reply. Do you have an example? Commented Dec 6, 2019 at 0:15
  • for FutureBuilder ? on medium.com You can find tons of them. Maybe This one be helpful. If you need help to refactor your code, I'd be happy to help. Commented Dec 6, 2019 at 0:23
  • I tried that and I think I did it correctly. Now, it's printing the result, but just not displaying it into a Text widget. getResultCount () async { var x; var value = await DBProvider.db.getUnCompletedTaskCount().then((val) { x = val; }); print(x.toString() + "test"); return x; } Commented Dec 6, 2019 at 0:30

3 Answers 3

9

I don't know how You're going to use this function, If you need it to get number of completed tasks and then draw something on screen(Like show a list of them or show number of them), I myself use FutureBuilder,Like Below:

FutureBuilder<int>(
  future: getResultCount(),
  builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
    if(snapshot.hasData){
      return Text('Number Of completed : ${snapshot.data}');
    }
    return Container();
  },
)

So when it completely fetch it, it shows a Text Widget, Otherwise it shows nothing(empty Container)

But if you need it in lets say an equation or something, you can do something like this:

  await getResultCount().then((val){
      // do some operation
    });

But again it totaly depends on your code and need.

When you need result of a future function in your code use await before it.

Like getResultCount() use await when You need to use the result of it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. Now, it's saying "build function returned null". Agh, I'm banging my head with this...
You should use FutureBuilder as a Widget in somewhere like child,... Because it's like normal widgets, if data isn't fetched completely, it returns Container(), otherwise the Text widget. so if you're using it in Build function, use return before FutureBuilder.
This solved my problem. I left it for a few days, and the solution seemed to have now worked. Thank you!
thank you. I had a similar instance only that I had not used the await key word
4
  • Error shows that you are trying to use value of Future<dynamic> as String which it won't let you because value haven't returned yet.

  • So basically what we have to do here is wait for value to be returned by using then keyword.

Like here.

 getResultCount().then((value){

    });

Comments

2

On Net Ninja flutter tutorial : User.signInAnon() returns "Future dynamic" ... you should add await in sign_in.dart

  onPressed: () async {
            dynamic result = **await** _auth.signInAnon();

            if (result == null) {
              print('Not signed In');
            } else {
              print('Signed In ');
              print(result);
            }
          },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.