_futureData
用于从函数中检索FutureBuilder
值后。_loadPhobias()
entry_screen.dart
Future _futureData;
final TextEditingController _textEditingController = TextEditingController();
_loadPhobias()
功能似乎没有任何问题。
entry_screen.dart
Future<List<String>> _loadPhobias() async =>
await rootBundle.loadString('assets/phobias.txt').then((phobias) {
List _listOfAllPhobias = [];
List<String> _listOfSortedPhobias = [];
_textEditingController.addListener(() {
...
});
return _listOfSortedPhobias;
});
@override
void initState() {
super.initState();
_futureData = _loadPhobias();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextField(
// When the value is changed, the value returned from the _loadPhobias will also change. So I want the FutureBuilder to be rebuilt.
onChanged: (text) { setState(() => _futureData = _loadPhobias()) },
),
),
body: FutureBuilder(
future: _futureData,
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) => Column(
children: <Widget>[
PhobiasCard(sentence: snapshot.data[index]),
)
],
))
: Center(
child: CircularProgressIndicator(),
);
},
),
),
);
}
这是我得到的错误:
FlutterError (setState() 回调参数返回了一个 Future。
_EntryScreenState#51168 上的 setState() 方法是使用返回 Future 的闭包或方法调用的。也许它被标记为“异步”。
与其在 setState() 调用中执行异步工作,不如先执行该工作(不更新小部件状态),然后在 setState() 调用中同步更新状态。)