为什么FutureBuilder在Flutter中没有接到电话? [英] Why FutureBuilder doesn't get call in Flutter?
问题描述
我的屏幕上有一个按钮,每当我单击_fetchPost()
时,我都会呼叫它.此方法调用fetchPost()
.在var post = ...
行上设置断点时,我能够看到我的解析对象.但是,我的问题是builder
不会被调用.
I have a button in my screen and I am calling _fetchPost()
whenever I click it. This method calls fetchPost()
. I am able to see my parsed object when I set break point on var post = ...
line. However, my problem is builder
won't be called.
我是Flutter的新手,所以我的代码错了吗?
I am very new in Flutter, so is my code wrong?
void _fetchPost() {
FutureBuilder<Post>(
future: fetchPost(),
builder: (BuildContext context, AsyncSnapshot<Post> snapshot) {
if (snapshot.hasData) {
String res = snapshot.data.parm1.toString() + snapshot.data.op + snapshot.data.parm2.toString();
setState(() {
_result = res;
});
} else if (snapshot.hasError) {
setState(() {
_result = snapshot.error;
});
}
// By default, show a loading spinner
return CircularProgressIndicator();
},
);
}
Future<Post> fetchPost() async {
final response = await http.get('http://test.ethorstat.com/test.ashx');
if (response.statusCode == 200) {
// If server returns an OK response, parse the JSON
var post = Post.fromJason(json.decode(response.body));
return post;
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post!');
}
}
推荐答案
FutureBuilder
是小工具.这意味着要使其正常工作,您需要先将其插入小部件树.
FutureBuilder
is a widget. This means that in order for it to work, you need to insert it into the widget tree first.
通过将FutureBuilder
作为子级返回到另一个窗口小部件或直接返回到窗口小部件的build
函数,将FutureBuilder
添加到窗口小部件树:
Either you add your FutureBuilder
to the widget tree by returning it as a child to another widget or directly to a build
function of a widget:
class FetchWidget extends StatelessWidget {
@override
Widget build(BuildContext context) => FutureBuilder<Post>(
future: fetchPost(),
builder: (BuildContext context, AsyncSnapshot<Post> snapshot) {
if (snapshot.hasData) {
String res = snapshot.data.parm1.toString() + snapshot.data.op + snapshot.data.parm2.toString();
setState(() { // setState will not work here, I only used this StatelessWidget to show what I mean with inserting it into the build tree
_result = res;
});
} else if (snapshot.hasError) {
setState(() {
_result = snapshot.error;
});
}
// By default, show a loading spinner
return CircularProgressIndicator();
},
);
Future<Post> fetchPost() async {
final response = await http.get('http://test.ethorstat.com/test.ashx');
if (response.statusCode == 200) {
// If server returns an OK response, parse the JSON
var post = Post.fromJason(json.decode(response.body));
return post;
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post!');
}
}
}
这不会编译,因为我只将您的代码复制到了没有setState
的StatelessWidget
中.我只想通过说将[...]添加到小部件树" 来表明我的意思.
This will not compile because I only copied your code into a StatelessWidget
, which does not have setState
. I only wanted to show what I mean by saying "add [...] to the widget tree".
或者您可以将常规async
await
用于尝试执行的操作:
Or you can just use regular async
await
for what you are trying to do:
void _fetchPost() async {
final Post fetchedPost = await fetchPost();
final String res = fetchedPost.parm1.toString() + fetchedPost.op + fetchedPost.parm2.toString();
setState(() {
_result = res;
});
}
我不确定您要如何实现fetchPost
,但是如果要使用FutureBuilder
,则需要将其插入小部件树并将小部件返回到builder
.
I am not sure how you want to implement your fetchPost
, but if you want to use FutureBuilder
, you need to insert it into the widget tree and return a widget to the builder
.
这篇关于为什么FutureBuilder在Flutter中没有接到电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!