Jonathon
Jonathon

Reputation: 1591

Keep returning "Instance of 'Future<dynamic>" instead of String (Flutter)

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.

    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;
  }
    getResultCount () async {
    final value = await DBProvider.db.getUnCompletedTaskCount();
    return value;
  }
    getResultCount().toString()

Upvotes: 4

Views: 21640

Answers (3)

Shadeer
Shadeer

Reputation: 89

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

Upvotes: 2

Vrushi Patel
Vrushi Patel

Reputation: 2431

  • 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){

    });

Upvotes: 4

NoobN3rd
NoobN3rd

Reputation: 1261

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.

Upvotes: 9

Related Questions