Flutter 错误:当从函数 Future<bool> 获取值时,无法将类型为“Future<bool>”的值分配给类型为“bool”的变量

时间:2021-03-23 16:27:23

标签: flutter dart boolean future getvalue

我在获取值表单函数Future时遇到问题
我在下面有一个功能

Future<bool> checkstr(String str) async {
     bool result = await check(str);
     if (result == null) {
         return false;
     } else {
         return true;
     }
}

当我得到并检查值时遇到错误

Expanded(
      child: model.isBusy
           ? CircularProgressIndicator()
           : ListView.builder(   
                  itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                          model.checkstr(str)       //<------ error this line
                          ? title: Text('true')
                          : title: Text('false'),
                    }),
          ),

错误

 Flutter Error: A value of type 'Future<bool>' can't be assigned to a variable of type 'bool'

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您必须使用 FutureBuilder,因为 checkstr()未来

Expanded(
    child: model.isBusy
        ? CircularProgressIndicator()
        : ListView.builder(itemBuilder: (BuildContext context, int index) {
            return FutureBuilder(
              future: checkstr(str),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting)
                  return CircularProgressIndicator();
                return ListTile(
                    title: Text(snapshot.data ? 'true' : 'false'));
              },
            );
          }));