【发布时间】:2018-06-29 12:16:01
【问题描述】:
我已经看过了:https://stackoverflow.com/a/49146503/1757321
遵循解决方案,但在我的情况下不起作用。
今天下午给我一些启示
Future<String> loadInterest() async {
print('Going to load interests');
final whenDone = new Completer();
SharedPreferences prefs = await SharedPreferences.getInstance();
final token = await prefs.getString('token');
print('The token ${token}');
await this.api.interests(token).then((res) {
// print('The response: ${res['interests']}'); <-- this prints response alright. Data is coming.
whenDone.complete(res['interests']);
});
return whenDone.future;
}
然后我尝试在未来的构建器中使用上述Future:
new FutureBuilder(
future: loadInterest(),
builder: (BuildContext context, snapshot) {
return snapshot.connectionState == ConnectionState.done
? new Wrap(
children: InterestChips(snapshot.data),
)
: Center(child: CircularProgressIndicator());
},
),
InterestChips(...) 在哪里:
InterestChips(items) {
print('Interest Chips ${items}');
List chipList;
for (Object item in items) {
chipList.add(Text('${item}'));
}
return chipList;
}
但是,我总是得到null 作为快照,这意味着Future 的loadInterest() 没有返回任何内容。
如果我正确理解了这个答案,那么我就是按照这个思路做的:https://stackoverflow.com/a/49146503/1757321
【问题讨论】:
-
发送您发送 HTTP 请求的链接。