在FutureBuilder完成执行之前,将处理变量相关的小部件。
使用以下代码'在空值上调用减法'出现错误。
-是否可以在布局中创建其余窗口小部件之前获取 word (问题)变量?
List<int> word = [];
int length;
var rng = new Random();
body: Column(children: [
Container(
child: FutureBuilder<Quote>(
future: getQuote(),
builder: (BuildContext context, AsyncSnapshot<Quote> snapshot) {
if (snapshot.hasData) {
word = snapshot.data.tag.runes.toList();
word.shuffle();
length = word.length;
return Center(
child: Column(
children: <Widget>[
Text(snapshot.data.tag),
Image.network(
'${snapshot.data.url}',
), //displays the quote's author
],
),
);
} else {
return CircularProgressIndicator();
},
),
),
Container(
height:75,
child: GridView.count(
crossAxisCount: 5,
children: List.generate(5, (int index) {
return Center(
child: FlatButton(
child: Text(
String.fromCharCode(
下一行引发错误并为空
index = (length-word.length)<3 ? word.removeAt(rng.nextInt(word.length))
: 65+rng.nextInt(25)
),
...
该功能,可从url加载数据
Future<Quote> getQuote() async {
var API_KEY = '12892554-73418a404928f2a4e42121633';
final object = generateNoun().take(1);
String url = "https://pixabay.com/api/?
key="+API_KEY+"&q="+Uri.encodeFull(object.toString());
final response = await http.get(url);
if (response.statusCode == 200) {
return Quote.fromJson(json.decode(response.body));
}
}
答案 0 :(得分:0)
已解决,方法是将整个布局树打包到FutureBuilder中,然后将整个内容放置在Scaffold中的 body 中。
@override
Widget build(BuildContext context) {
var my_layout = FutureBuilder<Quote>(
future: getQuote(), //sets the getQuote method as the expected Future
builder: (BuildContext context, AsyncSnapshot<Quote> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return new Text('No preferences');
case ConnectionState.waiting:
return CircularProgressIndicator();
default:
if (snapshot.hasError) {
return InkWell(
child: Padding(
padding: const EdgeInsets.all(32.0),
child: Text("ERROR OCCURRED, Tap to retry !"),
),
onTap: () => setState(() {}));
}
else { // this runs when data is retrieved properly
// set values for all variables, which depend on FutureBuilder and required in rest widgets
// put ALL widgets, that were previously in body //
return Scaffold(
appBar: AppBar(
title: Text('Guess word'),
),
body: my_layout,
);
// close build method