text stringlengths 1 372 |
|---|
appBar: AppBar( |
title: text(title), |
), |
body: FutureBuilder<List<Photo>>( |
future: fetchPhotos(http.Client()), |
builder: (context, snapshot) { |
if (snapshot.haserror) { |
return const center( |
child: Text('An error has occurred!'), |
); |
} else if (snapshot.hasdata) { |
return PhotosList(photos: snapshot.data!); |
} else { |
return const center( |
child: CircularProgressIndicator(), |
); |
} |
}, |
), |
); |
} |
} |
class PhotosList extends StatelessWidget { |
const PhotosList({super.key, required this.photos}); |
final List<Photo> photos; |
@override |
widget build(BuildContext context) { |
return GridView.builder( |
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( |
crossAxisCount: 2, |
), |
itemCount: photos.length, |
itemBuilder: (context, index) { |
return Image.network(photos[index].thumbnailUrl); |
}, |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
persistence |
<topic_end> |
<topic_start> |
topics |
<topic_end> |
<topic_start> |
store key-value data on disk |
if you have a relatively small collection of key-values |
to save, you can use the shared_preferences plugin. |
normally, you would have to |
write native platform integrations for storing data on each platform. |
fortunately, the shared_preferences plugin can be used to |
persist key-value data to disk on each platform flutter supports. |
this recipe uses the following steps: |
info note |
to learn more, watch this short package of the week video |
on the shared_preferences package: |
<topic_end> |
<topic_start> |
1. add the dependency |
before starting, add the shared_preferences package as a dependency. |
to add the shared_preferences package as a dependency, |
run flutter pub add: |
<topic_end> |
<topic_start> |
2. save data |
to persist data, use the setter methods provided by the |
SharedPreferences class. setter methods are available for |
various primitive types, such as setInt, setBool, and setString. |
setter methods do two things: first, synchronously update the |
key-value pair in memory. then, persist the data to disk. |
<code_start> |
// load and obtain the shared preferences for this app. |
final prefs = await SharedPreferences.getInstance(); |
// save the counter value to persistent storage under the 'counter' key. |
await prefs.setInt('counter', counter); |
<code_end> |
<topic_end> |
<topic_start> |
3. read data |
to read data, use the appropriate getter method provided by the |
SharedPreferences class. for each setter there is a corresponding getter. |
for example, you can use the getInt, getBool, and getString methods. |
<code_start> |
final prefs = await SharedPreferences.getInstance(); |
// try reading the counter value from persistent storage. |
// if not present, null is returned, so default to 0. |
final counter = prefs.getInt('counter') ?? 0; |
<code_end> |
note that the getter methods throw an exception if the persisted value |
has a different type than the getter method expects. |
<topic_end> |
<topic_start> |
4. remove data |
to delete data, use the remove() method. |
<code_start> |
final prefs = await SharedPreferences.getInstance(); |
// remove the counter key-value pair from persistent storage. |
await prefs.remove('counter'); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.