Reputation: 866
I want to display an object retrived calling an api
on button pressed.
I want to update FutureBuilder
widget when I press the floatingActionButton
:
class PostScreen extends StatefulWidget {
const PostScreen();
@override
State<StatefulWidget> createState() => _PostScreenState();
}
class _PostScreenState extends State<PostScreen> {
Future<Post> _post;
PostApi _postApi;
@override
void initState() {
super.initState();
_postApi = Provider.of<PostApi>(context, listen: false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(onPressed: () {
setState((){
_post = _postApi.fetchPost();
}); }),
appBar: AppBar(title: const Text('Post')),
body:
FutureBuilder<Post>(
future: _post,
builder: (BuildContext context, AsyncSnapshot<Post> snapshot) {
return snapshot.connectionState == ConnectionState.done
? snapshot.hasData
? _buildRoot(snapshot.data)
: Text(snapshot.error.toString());
: const Text('...');
}),
) ;
}
class Post {
Post.fromJson(this.data);
dynamic data;
}
class PostApi{
PostApi(this._httpManager);
final HttpManager _httpManager;
Future<Post> fetchPost() async {
final http.Response response =
await _httpManager.get('https://jsonplaceholder.typicode.com/posts/1');
if (response.statusCode == 200) {
return Post.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}
Everythings work but I have an issue when in the fetchPost
method there is an exception. For example:
Future<Post> fetchPost() async {
final http.Response response =
await _httpManager.get('https://jsonplaceholder.typicode.com/posts/1');
throw Exception('Failed to load post');
}
}
In this case the futureBuilder
connectionState is in waiting status instead of done.
What I'm doing wrong?
Upvotes: 3
Views: 5082
Reputation: 54367
Your code snippet has syntax error, I am unable to reproduce
Your throw Exception
works fine with switch case
You can copy paste run full code below
code snippet
builder: (BuildContext context, AsyncSnapshot<Post> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Input a URL to start');
case ConnectionState.waiting:
print("waiting");
return Text('waiting');
case ConnectionState.active:
print("active");
return Text('active');
case ConnectionState.done:
if (snapshot.hasError) {
print("has Error");
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.red),
);
} else {
return Text(snapshot.data.data.toString());
}
}
})
working demo
full code
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Post {
Post.fromJson(this.data);
dynamic data;
}
class PostApi {
PostApi();
//final HttpManager _httpManager;
Future<Post> fetchPost() async {
final http.Response response =
await http.get('https://jsonplaceholder.typicode.com/posts/1');
throw Exception('Failed to load post');
if (response.statusCode == 200) {
return Post.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}
}
class PostScreen extends StatefulWidget {
const PostScreen();
@override
State<StatefulWidget> createState() => _PostScreenState();
}
class _PostScreenState extends State<PostScreen> {
Future<Post> _post;
PostApi _postApi = PostApi();
@override
void initState() {
super.initState();
_post = _postApi.fetchPost();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(onPressed: () {
setState(() {
_post = _postApi.fetchPost();
});
}),
appBar: AppBar(title: const Text('Post')),
body: FutureBuilder<Post>(
future: _post,
builder: (BuildContext context, AsyncSnapshot<Post> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Input a URL to start');
case ConnectionState.waiting:
print("waiting");
return Text('waiting');
case ConnectionState.active:
print("active");
return Text('active');
case ConnectionState.done:
if (snapshot.hasError) {
print("has Error");
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.red),
);
} else {
return Text(snapshot.data.data.toString());
}
}
}),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: PostScreen(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Upvotes: 5