text stringlengths 1 372 |
|---|
userId: userId, |
id: id, |
title: title, |
), |
_ => throw const FormatException('Failed to load album.'), |
}; |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
convert the http.Response to an album |
now, use the following steps to update the fetchAlbum() |
function to return a Future<Album>: |
<code_start> |
Future<Album> fetchAlbum() async { |
final response = await http |
.get(uri.parse('https://jsonplaceholder.typicode.com/albums/1')); |
if (response.statuscode == 200) { |
// if the server did return a 200 OK response, |
// then parse the JSON. |
return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>); |
} else { |
// if the server did not return a 200 OK response, |
// then throw an exception. |
throw Exception('Failed to load album'); |
} |
} |
<code_end> |
hooray! |
now you’ve got a function that fetches an album from the internet. |
<topic_end> |
<topic_start> |
4. fetch the data |
call the fetchAlbum() method in either the |
initState() or didChangeDependencies() |
methods. |
the initState() method is called exactly once and then never again. |
if you want to have the option of reloading the API in response to an |
InheritedWidget changing, put the call into the |
didChangeDependencies() method. |
see state for more details. |
<code_start> |
class _MyAppState extends State<MyApp> { |
late Future<Album> futureAlbum; |
@override |
void initState() { |
super.initState(); |
futureAlbum = fetchAlbum(); |
} |
// ··· |
} |
<code_end> |
this future is used in the next step. |
<topic_end> |
<topic_start> |
5. display the data |
to display the data on screen, use the |
FutureBuilder widget. |
the FutureBuilder widget comes with flutter and |
makes it easy to work with asynchronous data sources. |
you must provide two parameters: |
note that snapshot.hasData only returns true |
when the snapshot contains a non-null data value. |
because fetchAlbum can only return non-null values, |
the function should throw an exception |
even in the case of a “404 not found” server response. |
throwing an exception sets the snapshot.hasError to true |
which can be used to display an error message. |
otherwise, the spinner will be displayed. |
<code_start> |
FutureBuilder<Album>( |
future: futureAlbum, |
builder: (context, snapshot) { |
if (snapshot.hasdata) { |
return text(snapshot.data!.title); |
} else if (snapshot.haserror) { |
return text('${snapshot.error}'); |
} |
// by default, show a loading spinner. |
return const CircularProgressIndicator(); |
}, |
) |
<code_end> |
<topic_end> |
<topic_start> |
why is fetchAlbum() called in initState()? |
although it’s convenient, |
it’s not recommended to put an API call in a build() method. |
flutter calls the build() method every time it needs |
to change anything in the view, |
and this happens surprisingly often. |
the fetchAlbum() method, if placed inside build(), is repeatedly |
called on each rebuild causing the app to slow down. |
storing the fetchAlbum() result in a state variable ensures that |
the future is executed only once and then cached for subsequent |
rebuilds. |
<topic_end> |
<topic_start> |
testing |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.