1
Future<int> getCountTodo(int taskId) async {
   Database db = await this.db;
   List<Map<String, dynamic>> x = await db.rawQuery('SELECT COUNT (*) from $todoTable where taskId=$taskId');
   // List<Map<String, dynamic>> x = await db.rawQuery('SELECT  COUNT(*),t.title FROM todo_table td, task_table t WHERE t.id=td.taskId GROUP BY taskId');
   int result = Sqflite.firstIntValue(x);
   print('Todo inside task: $result');
   return result;
 }

Text (
 // '${1}'+" ITEM", 
 DatabaseHelper.instance.getCountTodo(task.tasklist[index].id).toString(),
 style: TextStyle(
   fontSize: 14.0,
   color: Colors.grey,
   fontWeight: FontWeight.bold),
)

How can i get the count result here in text widget. I'm getting Instance of 'Future'.

Valentin Vignal
  • 6,151
  • 2
  • 33
  • 73
  • 1
    declare class variable and assign value to it by calling your db method ! if it is inside listview or gridview then make builder asyn and use keyword await before db method call – Naveen Avidi Jul 31 '21 at 04:24
  • Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – jamesdlin Aug 01 '21 at 11:26
  • See [ask] to improve this question. – Alex.F Aug 01 '21 at 17:42

1 Answers1

2

It's better practice to use a future builder here:

FutureBuilder(
 future: DatabaseHelper.instance.getCountTodo(task.tasklist[index].id),
 builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
       switch (snapshot.connectionState) {
         case ConnectionState.waiting: return Text('Loading....');
         default:
           if (snapshot.hasError)
              return Text('Error: ${snapshot.error}');
           else if (snapshot.data==null)
              return Text('Loading....');
           else
          return Text('${snapshot.data}');
        }
      },
    )


)
Amon C
  • 1,710
  • 9
  • 17