【发布时间】:2019-05-22 20:51:40
【问题描述】:
我需要在我可以遵循的两种方法中选择最佳方法。
我有一个使用 sqflite 保存数据的 Flutter 应用程序,在数据库中我有两个表:
员工:
+-------------+-----------------+------+
| employee_id | employee_name |dep_id|
+-------------+-----------------+------+
| e12 | Ada Lovelace | dep1 |
+-------------+-----------------+------+
| e22 | Albert Einstein | dep2 |
+-------------+-----------------+------+
| e82 | Grace Hopper | dep3 |
+-------------+-----------------+------+
SQL:
CREATE TABLE Employee(
employee_id TEXT NOT NULL PRIMARY KEY,
employee_name TEXT NOT NULL ,
dep_id TEXT,
FOREIGN KEY(dep_id) REFERENCES Department(dep_id)
ON DELETE SET NULL
);
部门:
+--------+-----------+-------+
| dep_id | dep_title |dep_num|
+--------+-----------+-------+
| dep1 | Math | dep1 |
+--------+-----------+-------+
| dep2 | Physics | dep2 |
+--------+-----------+-------+
| dep3 | Computer | dep3 |
+--------+-----------+-------+
SQL:
CREATE TABLE Department(
dep_id TEXT NOT NULL PRIMARY KEY,
dep_title TEXT NOT NULL ,
dep_num INTEGER,
);
我需要显示存储在 Employee 表中的部门的ListGrid。我应该查看 Employee 表并从中获取部门 id,这很容易,但是在获取 dep_id 之后,我需要从这些 id 中制作一张卡片,所以我需要来自 Department 的信息强>表。
我从 Emplyee 表中获取的那些 id 的完整信息在 Department 表中。
每个表有数千行。
我有一个数据库助手类来连接数据库:
DbHelper 是这样的:
Future<List<String>> getDepartmentIds() async{
'fetch all dep_id from Employee table'
}
Future<Department> getDepartment(String id) async{
'fetch Department from Department table for a specific id'
}
Future<List<Department>> getEmployeeDepartments() async{
'''1.fetch all dep_id from Employee table
2.for each id fetch Department records from Department table'''
var ids = await getDepartmentIds();
List<Departments> deps=[];
ids.forEach((map) async {
deps.add(await getDepartment(map['dep_id']));
});
}
有两种方法:
第一个:
在 dbhelper 中定义一个函数,该函数返回 Employee 表中的所有
dep_id(getDepartmentIds以及另一个返回该特定 ID 的部门对象(模型)的函数。(@987654332 @)现在我需要两个
FutureBuilder,一个用于获取ID,另一个用于获取部门模型。
第二个:
- 定义一个函数,该函数首先获取 id,然后在该函数内部将每个 id 映射到部门模型。(
getEmployeeDepartments) - 所以我需要一个
FutureBuilder。
哪个更好??
我应该让 FutureBuilders 处理它,还是应该向dbHelper 施加压力来处理它?
如果我使用第一种方法,那么我必须(据我所知!)放置第二个未来调用(基于它的 id 获取 Department Object(model) 的那个( getDepartment)) 在 build 函数上,建议不要这样做。
第二个的问题是它在dbHelper 中做了很多嵌套调用。
我使用ListView.builder 来提高性能。
我用一些数据检查了两者,但无法确定哪一个更好。我想这取决于颤振和 sqlite(sqflite)。
哪个更好或有更好的方法?
【问题讨论】:
标签: database sqlite flutter flutter-layout sqflite