本文详细介绍了Flutter数据存储教程,涵盖本地存储方式如Shared Preferences、SQLite数据库和文件系统,并通过实战示例帮助开发者理解如何在Flutter应用中实现数据存储。文章还探讨了数据存储的最佳实践和注意事项,确保应用高效、安全。
Flutter 是由 Google 开发的一个开源 UI 框架,它允许开发者使用一套代码来构建跨平台的应用程序,包括 iOS 和 Android。Flutter 使用 Dart 语言编写,它提供了一个丰富的组件库,使得开发者能够创建高性能、美观的应用程序。
在移动应用开发中,数据存储是指将应用程序的数据保存到设备上,以便在应用程序启动后可以继续使用这些数据,或在用户离开应用后再返回时能够恢复状态。数据存储对于提供流畅的用户体验和功能完整性是至关重要的。
Shared Preferences
是一个轻量级的键值对存储系统,适用于存储简单数据类型,如布尔型、整型、浮点型和字符串。
import 'package:shared_preferences/shared_preferences.dart'; void savePreferences() async { final prefs = await SharedPreferences.getInstance(); await prefs.setString('name', 'John Doe'); await prefs.setInt('age', 30); await prefs.setDouble('height', 175.5); await prefs.setBool('isStudent', false); } void readPreferences() async { final prefs = await SharedPreferences.getInstance(); String name = prefs.getString('name') ?? 'No name'; int age = prefs.getInt('age') ?? 0; double height = prefs.getDouble('height') ?? 0.0; bool isStudent = prefs.getBool('isStudent') ?? false; print('Name: $name, Age: $age, Height: $height, Is Student: $isStudent'); }
SQLite
是一个轻量级的数据库系统,适用于存储结构化的数据。它支持 SQL 语言,可以进行复杂的查询。
import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; Future<void> createDatabase() async { final database = await openDatabase( join(await getDatabasesPath(), 'user.db'), onCreate: (db, version) async { await db.execute(''' CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL ) '''); }, version: 1, ); return database; } Future<void> insertUser(Database db, String name, int age) async { await db.insert( 'users', {'name': name, 'age': age}, conflictAlgorithm: ConflictAlgorithm.replace, ); } Future<List<Map<String, dynamic>>> getUsers(Database db) async { final List<Map<String, dynamic>> maps = await db.query('users'); return maps; } Future<void> updateUser(Database db, int id, String name, int age) async { await db.update( 'users', {'name': name, 'age': age}, where: 'id = ?', whereArgs: [id], ); } Future<void> deleteUser(Database db, int id) async { await db.delete( 'users', where: 'id = ?', whereArgs: [id], ); } Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); final db = await createDatabase(); await insertUser(db, 'John Doe', 30); await insertUser(db, 'Jane Doe', 25); await updateUser(db, 1, 'John Doe Updated', 31); await deleteUser(db, 2); final users = await getUsers(db); print(users); runApp(MyApp()); }
文件系统可以用来存储文件,如图片、视频、文本等。Flutter 提供了 path_provider
插件来帮助获取应用内和应用外的文件路径。
import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; import 'dart:io'; Future<void> writeToFile(String content) async { final directory = await getApplicationDocumentsDirectory(); final file = File(join(directory.path, 'example.txt')); await file.writeAsString(content); } Future<String> readFromFile() async { final directory = await getApplicationDocumentsDirectory(); final file = File(join(directory.path, 'example.txt')); return await file.readAsString(); } Future<void> main() async { await writeToFile('Hello, world!'); String content = await readFromFile(); print(content); }
flutter create shared_preferences_example
来创建一个新的 Flutter 项目。cd shared_preferences_example
。lib/main.dart
文件。在 pubspec.yaml
文件中添加 shared_preferences
插件:
dependencies: flutter: sdk: flutter shared_preferences: ^2.0.6
运行 flutter pub get
来安装插件。
在 main.dart
文件中添加以下代码:
import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: HomeScreen(), ); } } class HomeScreen extends StatefulWidget { @override _HomeScreenState createState() => _HomeScreenState(); } class _HomeScreenState extends State<HomeScreen> { TextEditingController _controller = TextEditingController(); Future<void> saveData() async { final prefs = await SharedPreferences.getInstance(); prefs.setString('username', _controller.text); } Future<void> readData() async { final prefs = await SharedPreferences.getInstance(); final username = prefs.getString('username') ?? 'No username'; ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Username: $username')), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Shared Preferences Example'), ), body: Padding( padding: EdgeInsets.all(16), child: Column( children: [ TextField( controller: _controller, decoration: InputDecoration(labelText: 'Enter your name'), ), SizedBox(height: 20), ElevatedButton( onPressed: saveData, child: Text('Save Data'), ), SizedBox(height: 20), ElevatedButton( onPressed: readData, child: Text('Read Data'), ), ], ), ), ); } }
在 main.dart
文件中添加以下代码:
import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; Future<void> createDatabase() async { final database = await openDatabase( join(await getDatabasesPath(), 'user.db'), onCreate: (db, version) async { await db.execute(''' CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL ) '''); }, version: 1, ); return database; } Future<void> insertUser(Database db, String name, int age) async { await db.insert( 'users', {'name': name, 'age': age}, conflictAlgorithm: ConflictAlgorithm.replace, ); } Future<List<Map<String, dynamic>>> getUsers(Database db) async { final List<Map<String, dynamic>> maps = await db.query('users'); return maps; } Future<void> updateUser(Database db, int id, String name, int age) async { await db.update( 'users', {'name': name, 'age': age}, where: 'id = ?', whereArgs: [id], ); } Future<void> deleteUser(Database db, int id) async { await db.delete( 'users', where: 'id = ?', whereArgs: [id], ); }
在 main.dart
文件中继续添加以下代码:
void main() async { WidgetsFlutterBinding.ensureInitialized(); final db = await createDatabase(); await insertUser(db, 'John Doe', 30); await insertUser(db, 'Jane Doe', 25); final users = await getUsers(db); print(users); runApp(MyApp()); }
在 main.dart
文件中继续添加以下代码:
void main() async { WidgetsFlutterBinding.ensureInitialized(); final db = await createDatabase(); await insertUser(db, 'John Doe', 30); await insertUser(db, 'Jane Doe', 25); await updateUser(db, 1, 'John Doe Updated', 31); final users = await getUsers(db); print(users); runApp(MyApp()); }
在 main.dart
文件中继续添加以下代码:
void main() async { WidgetsFlutterBinding.ensureInitialized(); final db = await createDatabase(); await insertUser(db, 'John Doe', 30); await insertUser(db, 'Jane Doe', 25); await deleteUser(db, 2); final users = await getUsers(db); print(users); runApp(MyApp()); }
在 main.dart
文件中添加以下代码:
import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; Future<String> getApplicationDocumentsDirectoryPath() async { final directory = await getApplicationDocumentsDirectory(); return directory.path; } Future<void> writeToFile(String content) async { final directory = await getApplicationDocumentsDirectory(); final file = File(join(directory.path, 'example.txt')); await file.writeAsString(content); } Future<String> readFromFile() async { final directory = await getApplicationDocumentsDirectory(); final file = File(join(directory.path, 'example.txt')); return await file.readAsString(); }
在 main.dart
文件中继续添加以下代码:
void main() async { final content = 'Hello, world!'; await writeToFile(content); final readContent = await readFromFile(); print(readContent); runApp(MyApp()); }
在 main.dart
文件中继续添加以下代码:
void main() async { final path = await getApplicationDocumentsDirectoryPath(); print('Path: $path'); runApp(MyApp()); }
Future<void> batchOperation(Database db) async { await db.transaction((txn) async { await txn.insert('users', {'name': 'John Doe', 'age': 30}); await txn.insert('users', {'name': 'Jane Doe', 'age': 25}); }); }
import 'package:cryptography/cryptography.dart'; Future<void> encryptData(String data) async { final key = await Key.fromUtf8('secret_key'); final iv = await aes256gcm.newiv(); final encrypted = await aes256gcm.encrypt(utf8.encode(data), iv: iv, key: key); print(base64.encode(encrypted.cipherText)); } Future<void> decryptData(String encryptedData) async { final key = await Key.fromUtf8('secret_key'); final iv = await base64.decode('...'); final decrypted = await aes256gcm.decrypt(base64.decode(encryptedData), iv: iv, key: key); print(utf8.decode(decrypted)); }
通过以上实践示例和最佳实践,开发者可以更好地理解和使用 Flutter 中的数据存储功能,构建更高效、安全的应用程序。