I have three models in my project, LibraryModel
, LibraryContentsModel
, and BooksModel
.
LibraryModel
: a model for local JSON file.
BooksModel
: a model for SQLite database.
LibraryContentsModel
: a model I created for search and filter into SQLite database.
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,
});
}
Problem:
I need to insert data from SQLite Database into the booksList
parameter in LibraryContetnsModel
which is already defined named of table SQLite in LibraryModel
, each table contains data in order.
Fetch Code:
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);
}
Edit
I need Like this example:
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"
)
How can I do that?
CodePudding user response:
Why don't use just
LibraryContentsModel(
...
bookList: booklist, // already fetched
),