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

如何在文本小部件中获取计数结果。我得到了“未来”的实例。

4

1 回答 1

2

在这里使用未来的构建器是更好的做法:

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


)
于 2021-07-31T04:26:52.340 回答