我的專案中有三個模型LibraryModel,LibraryContentsModel、 和BooksModel。
LibraryModel: 本地 JSON 檔案的模型。
BooksModel: SQLite 資料庫的模型。
LibraryContentsModel:我為搜索和過濾到 SQLite 資料庫而創建的模型。
LibraryModel:
class LibraryModel {
final String? title;
final String? fullTitle;
final String? subject;
final int? id;
final int? count;
final String? image;
final String? dbName;
final String? part;
bool? isSelected;
LibraryModel({
this.title,
this.fullTitle,
this.subject,
this.id,
this.count,
this.image,
this.dbName,
this.part,
this.isSelected,
});
}
BooksModel:
class BooksModel {
final int? id;
final String? pageTitle;
final String? pageText;
final String? footTitle;
BooksModel({
this.id,
this.pageTitle,
this.pageText,
this.footTitle,
});
}
LibraryContentsModel:
class LibraryContentsModel {
int? id;
String? title;
String? part;
String? dbName;
List<BooksModel>? booksList;
LibraryContentsModel({
this.id,
this.title,
this.part,
this.dbName,
this.booksList,
});
}
問題:
我需要將 SQLite 資料庫中的資料插入到已經定義名為 SQLite 的表的booksList引數中,每個表都按順序包含資料。LibraryContetnsModelLibraryModel
獲取代碼:
Future loadLibrary() async {
setState(() => isLoad = true);
try {
libraryList.addAll(await LibraryService.instance.getLibrary());
for (var item in libraryList) {
booksList
.addAll(await DatabaseService.instance.getBooks(item.dbName ?? ""));
libraryContentsList.add(
LibraryContentsModel(
id: item.id,
title: item.title,
part: item.part,
dbName: item.dbName,
// booksList: [booksList].expand((element) => element).toList(), <-- When I run this line get me exception
),
);
}
} catch (e) {
print(e.toString());
}
setState(() => isLoad = false);
}
編輯
我需要像這個例子:
LibraryModel:
id: 1
title: "Clean Code"
part: "2nd Edition"
dbName: "clean_code"
...
BooksModel:
id: 115
pageTitle: "Test Automation Pyramid"
pageText: "Professional developers employ the discipline of Test Driven Development...."
footTitle: "[COHN09] pp.311-312"
LibraryContentsModel:
id: LibraryModel.id = 1
title: LibraryModel.title = "Clean Code"
part: LibraryModel.part = "2nd Edition"
dbName: LibraryModel.dbName = "clean_code"
booksList: BooksModel(
id: 115
pageTitle: "Test Automation Pyramid"
pageText: "Professional developers employ the discipline of Test Driven Development...."
footTitle: "[COHN09] pp.311-312"
)
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
為什么不只使用
LibraryContentsModel(
...
bookList: booklist, // already fetched
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437566.html
上一篇:不好:FastApi回應問題
