flutter shared_preferences
时间: 2023-04-27 10:01:35 浏览: 83
Flutter中的shared_preferences是一个用于存储小量数据的插件,它可以将数据存储在本地设备上,例如用户的偏好设置、应用程序状态等。它是一个轻量级的解决方案,非常适合存储简单的键值对数据。使用shared_preferences非常简单,只需要在Flutter应用程序中添加依赖项,然后就可以开始使用它来存储和读取数据了。
相关问题
请使用flutter操作shared_preferences的例程
好的,这是一个使用 Flutter 操作 shared_preferences 的例程:
```dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
void initState() {
super.initState();
_loadCounter();
}
Future<void> _loadCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
_counter = (prefs.getInt('counter') ?? 0);
});
}
Future<void> _incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
_counter++;
prefs.setInt('counter', _counter);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Shared Preferences Demo"),
),
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.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
```
这个例程演示了如何在 Flutter 中使用 shared_preferences 库来读取和写入应用程序的持久化数据。在这个例子中,我们使用一个整型变量 `_counter` 来记录用户点击按钮的次数。在 `initState` 方法中,我们调用 `_loadCounter` 方法来从 shared_preferences 中读取 `_counter` 的值。在 `_incrementCounter` 方法中,我们调用 `prefs.setInt('counter', _counter)` 来将 `_counter` 的值写入 shared_preferences。每当用户点击按钮时,我们调用 `_incrementCounter` 方法来增加 `_counter` 的值并更新 UI。
注意:为了使用 shared_preferences 库,你需要在 `pubspec.yaml` 文件中添加以下依赖项:
```yaml
dependencies:
shared_preferences: ^2.0.5
```
flutter使用shared_preferences存储一个class结构
您可以将该class结构转换为JSON格式,并将其作为字符串存储在shared_preferences中。以下是一个简单的示例代码:
```dart
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class MyData {
String name;
int age;
bool isStudent;
MyData({this.name, this.age, this.isStudent});
factory MyData.fromJson(Map<String, dynamic> json) {
return MyData(
name: json['name'],
age: json['age'],
isStudent: json['isStudent'],
);
}
Map<String, dynamic> toJson() => {
'name': name,
'age': age,
'isStudent': isStudent,
};
}
// To save the data in shared preferences
Future<void> saveData(MyData data) async {
final prefs = await SharedPreferences.getInstance();
final jsonData = jsonEncode(data.toJson());
await prefs.setString('myData', jsonData);
}
// To fetch the data from shared preferences
Future<MyData> fetchData() async {
final prefs = await SharedPreferences.getInstance();
final jsonData = prefs.getString('myData');
if (jsonData != null) {
final data = MyData.fromJson(jsonDecode(jsonData));
return data;
}
return null;
}
```
您可以使用以上代码中的`saveData()`方法将数据保存在shared_preferences中,这里我们将数据转换为JSON格式字符串并将其存储在`myData`键下。`fetchData()`方法可以用于从shared_preferences中获取数据,并将其转换为`MyData`类的实例。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)