text
stringlengths
1
474
It’s a good idea to test code that persists data using shared_preferences.
To enable this, the package provides an
in-memory mock implementation of the preference store.To set up your tests to use the mock implementation,
call the setMockInitialValues static method in
a setUpAll() method in your test files.
Pass in a map of key-value pairs to use as the initial values.
<code_start>SharedPreferences.setMockInitialValues(<String, Object>{
'counter': 2,
});<code_end>
<topic_end>
<topic_start>
Complete example
<code_start>import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Shared preferences demo',
home: MyHomePage(title: 'Shared preferences demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
void initState() {
super.initState();
_loadCounter();
}
/// Load the initial counter value from persistent storage on start,
/// or fallback to 0 if it doesn't exist.
Future<void> _loadCounter() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_counter = prefs.getInt('counter') ?? 0;
});
}
/// After a click, increment the counter state and
/// asynchronously save it to persistent storage.
Future<void> _incrementCounter() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_counter = (prefs.getInt('counter') ?? 0) + 1;
prefs.setInt('counter', _counter);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times: ',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}<code_end>
<topic_end>
<topic_start>Read and write files
In some cases, you need to read and write files to disk.
For example, you might need to persist data across app launches,
or download data from the internet and save it for later offline use.To save files to disk on mobile or desktop apps,
combine the path_provider plugin with the dart:io library.This recipe uses the following steps:To learn more, watch this Package of the Week video
on the path_provider package:info Note
This recipe doesn’t work with web apps at this time.
To follow the discussion on this issue,
check out flutter/flutter issue #45296.<topic_end>
<topic_start>
1. Find the correct local path
This example displays a counter. When the counter changes,
write data on disk so you can read it again when the app loads.
Where should you store this data?The path_provider package
provides a platform-agnostic way to access commonly used locations on the
device’s file system. The plugin currently supports access to