text stringlengths 1 474 |
|---|
); |
} |
} |
class MyHomePage extends StatelessWidget { |
const MyHomePage({super.key, required this.title}); |
final String title; |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
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');<code_end> |
<topic_end> |
<topic_start> |
Supported types |
Although the key-value storage provided by shared_preferences is |
easy and convenient to use, it has limitations:<topic_end> |
<topic_start> |
Testing support |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.