我在获取值表单函数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'
请帮帮我。
答案 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'));
},
);
}));