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.
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!');
}
}