我需要在我可以遵循的两种方法之间选择最佳方法。
我有一个用于保存数据的 Flutter 应用程序sqflite
,在数据库中我有两个表:
员工:
+-------------+-----------------+------+
| 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,
);
我需要显示一个ListGrid
存储在Employee表中的部门。我应该查看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 中定义一个函数,该函数
dep_id
从Employee表中返回所有内容(getDepartmentIds
以及另一个返回该特定 id 的部门对象(模型)的函数。(getDepartment
)现在我需要两个
FutureBuilder
相互连接,一个用于获取 id,另一个用于获取部门模型。
第二个:
- 定义一个函数,首先获取 id,然后在该函数中,每个 id 都映射到部门模型。(
getEmployeeDepartments
) - 所以我需要一个
FutureBuilder
。
哪一个更好??我应该让 FutureBuilders 处理它还是应该施加压力dbHelper
来处理它?
如果我使用第一种方法,那么我必须(据我想象!)将第二个未来调用(基于它的 id( ) 获取DepartmentgetDepartment
Object(model) 的调用)放在build
函数上,建议不要这样做所以。
第二个的问题是它做了很多嵌套调用dbHelper
。
我用于ListView.builder
表演。
我用一些数据检查了两者,但无法弄清楚哪个更好。我想这取决于颤振和 sqlite(sqflite)。
哪个更好或有更好的方法?