在flutter中设计一个页面显示数据库某个数据表的数据
时间: 2023-11-12 13:04:22 浏览: 168
要在Flutter中显示数据库某个数据表的数据,需要使用Flutter提供的SQLite插件。以下是基本步骤:
1. 添加sqlite插件依赖
在pubspec.yaml文件中添加依赖:
```
dependencies:
flutter:
sdk: flutter
sqflite: ^1.3.1+1
```
然后运行`flutter packages get`安装插件。
2. 创建数据库和数据表
使用sqflite插件创建数据库和数据表。以下是一个示例:
```dart
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'package:path/path.dart';
class DatabaseHelper {
static final DatabaseHelper _instance = new DatabaseHelper.internal();
factory DatabaseHelper() => _instance;
final String tableData = 'dataTable';
final String columnId = 'id';
final String columnName = 'name';
final String columnValue = 'value';
static Database _db;
Future<Database> get db async {
if (_db != null) {
return _db;
}
_db = await initDb();
return _db;
}
DatabaseHelper.internal();
initDb() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'data.db');
var db = await openDatabase(path, version: 1, onCreate: _onCreate);
return db;
}
void _onCreate(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $tableData($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $columnName TEXT, $columnValue TEXT)');
}
Future<int> saveData(Data data) async {
var dbClient = await db;
int res = await dbClient.insert("$tableData", data.toMap());
return res;
}
Future<List<Data>> getData() async {
var dbClient = await db;
List<Map> maps = await dbClient.query("$tableData", columns: [columnId, columnName, columnValue]);
List<Data> dataList = [];
if (maps.length > 0) {
for (int i = 0; i < maps.length; i++) {
dataList.add(Data.fromMap(maps[i]));
}
}
return dataList;
}
}
class Data {
int id;
String name;
String value;
Data();
Data.fromMap(Map<String, dynamic> map) {
id = map['id'];
name = map['name'];
value = map['value'];
}
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
'name': name,
'value': value,
};
if (id != null) {
map['id'] = id;
}
return map;
}
}
```
3. 在页面中显示数据表数据
在页面中使用FutureBuilder和ListView.builder来异步获取和显示数据表中的数据。以下是一个示例:
```dart
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'database_helper.dart';
class DataListPage extends StatefulWidget {
@override
_DataListPageState createState() => _DataListPageState();
}
class _DataListPageState extends State<DataListPage> {
DatabaseHelper databaseHelper = DatabaseHelper();
List<Data> dataList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Data List'),
),
body: FutureBuilder<List<Data>>(
future: databaseHelper.getData(),
builder: (BuildContext context, AsyncSnapshot<List<Data>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
dataList = snapshot.data;
return ListView.builder(
itemCount: dataList.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(dataList[index].name),
subtitle: Text(dataList[index].value),
);
},
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
}
```
这个页面会显示数据库中所有数据。你可以将其作为一个模板,在其中修改数据表名称、字段名称等信息,以适应你的需求。
阅读全文