在Flutter中使用provider和sqflite数据库来保存收藏的项目到本地存储设备,可以按照以下步骤进行:
dependencies:
flutter:
sdk: flutter
provider: ^6.0.1
sqflite: ^2.0.0+4
然后运行flutter pub get
命令来获取依赖包。
Project
的类,包含项目的相关属性,如项目名称、描述等。class Project {
final String name;
final String description;
Project({required this.name, required this.description});
}
ProjectProvider
的类,继承自ChangeNotifier
,并在其中定义相关方法。import 'package:flutter/material.dart';
class ProjectProvider extends ChangeNotifier {
List<Project> _projects = [];
List<Project> get projects => _projects;
void addProject(Project project) {
_projects.add(project);
notifyListeners();
}
void removeProject(Project project) {
_projects.remove(project);
notifyListeners();
}
}
DatabaseHelper
的类,其中包含创建数据库、插入数据、查询数据等方法。import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class DatabaseHelper {
static final DatabaseHelper _instance = DatabaseHelper.internal();
factory DatabaseHelper() => _instance;
static late Database _database;
Future<Database> get database async {
if (_database != null) {
return _database;
}
_database = await initDatabase();
return _database;
}
DatabaseHelper.internal();
Future<Database> initDatabase() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'my_database.db');
return await openDatabase(
path,
version: 1,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE projects(id INTEGER PRIMARY KEY, name TEXT, description TEXT)',
);
},
);
}
Future<int> insertProject(Project project) async {
final db = await database;
return await db.insert('projects', project.toMap());
}
Future<List<Project>> getProjects() async {
final db = await database;
final List<Map<String, dynamic>> maps = await db.query('projects');
return List.generate(maps.length, (i) {
return Project(
name: maps[i]['name'],
description: maps[i]['description'],
);
});
}
}
ChangeNotifierProvider
并将其作为顶层Widget包装整个应用程序。void main() {
runApp(
ChangeNotifierProvider(
create: (context) => ProjectProvider(),
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: HomePage(),
);
}
}
Consumer
来获取ProjectProvider
的实例,并使用其中的方法和状态。import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final projectProvider = Provider.of<ProjectProvider>(context);
return Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: ListView.builder(
itemCount: projectProvider.projects.length,
itemBuilder: (context, index) {
final project = projectProvider.projects[index];
return ListTile(
title: Text(project.name),
subtitle: Text(project.description),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
projectProvider.removeProject(project);
},
),
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
final newProject = Project(
name: 'New Project',
description: 'Description',
);
projectProvider.addProject(newProject);
},
),
);
}
}
通过以上步骤,你可以在Flutter应用中使用provider和sqflite数据库来保存收藏的项目到本地存储设备中。每当添加或删除项目时,界面会自动更新以反映最新的状态。
T-Day
Techo Hub腾讯开发者技术沙龙城市站
Techo Day
云+社区技术沙龙[第17期]
云+社区技术沙龙[第25期]
DBTalk
腾讯云GAME-TECH沙龙
云+社区技术沙龙 [第31期]
第四期Techo TVP开发者峰会
DBTalk
领取专属 10元无门槛券
手把手带您无忧上云