是否有用于 flutter 的 SQLite 的 NoSQL 替代品?
Is there a NoSQL alternative to SQLite for flutter?
我正在构建一个独立的(没有 API 调用 MongoDB 或 Firebase 等)数据库管理至关重要的 flutter 应用程序。目前,我将其存储为 JSON,但效率很低。我不能使用 SQLite,因为数据非常嵌套,事实上,JSON 是存储我的数据的唯一方法。所以,我正在寻找 NoSQL 替代方案。
这是我要存储的数据模型。
lib/book_model.dart
@JsonSerializable()
class Book {
/// Do not change this. It's a primary key.
String bookLink;
String authors = null;
String thumbnail = null;
String bookName = null;
List<Chapter> totalChaptersList = [];
///todo: determine datatype
var currentChapter;
String summary = null;
double rating = 0.0;
List<String> genres = [];
Book({
this.bookLink,
this.authors,
this.thumbnail,
this.bookName,
this.totalChaptersList,
this.currentChapter,
this.summary,
this.rating,
this.genres,
});
Book.generateFromSearchBook(SearchBook searchBook) {
this.authors = searchBook.authors;
this.bookLink = searchBook.bookLink;
this.bookName = searchBook.bookName;
this.thumbnail = searchBook.thumbnail;
}
// @override
// String toString() {
// return "<$bookLink , $authors , $thumbnail , $bookName , $summary , $genres , $rating , $totalChaptersList , $currentChapter>";
// }
factory Book.fromJson(Map<String, dynamic> json) => _$BookFromJson(json);
Map<String, dynamic> toJson() => _$BookToJson(this);
}
@JsonSerializable()
class Chapter {
String name = null;
String date = null;
String chapterLink = null;
@JsonKey(defaultValue: false)
bool has_read = false;
List<Page> pages = [];
Chapter({
this.name,
this.date,
this.chapterLink,
this.has_read,
this.pages,
});
// @override
// String toString() {
// return "<$name , $date , $chapterLink , $has_read , $pages>";
// }
factory Chapter.fromJson(Map<String, dynamic> json) => _$ChapterFromJson(json);
Map<String, dynamic> toJson() => _$ChapterToJson(this);
}
@JsonSerializable()
class Page {
String pageLink = null;
int pageNumber = 0;
Page({
this.pageLink,
this.pageNumber,
});
// @override
// String toString() {
// return "<$pageLink , $pageNumber>";
// }
factory Page.fromJson(Map<String, dynamic> json) => _$PageFromJson(json);
Map<String, dynamic> toJson() => _$PageToJson(this);
}
我找到了这两个
我使用 Hive. It's fast and easy to use.you can use hive_generator 与 json_serializable 和 freezed 创建一个 HiveObject class.
示例:
@HiveType(typeId: 0)
class Person extends HiveObject {
@HiveField(0)
String name;
@HiveField(1)
int age;
}
您还可以使用 hive_box 将您的蜂巢箱变成可监听的并监听数据库变化。
示例:
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: Hive.box('settings').listenable(),
builder: (context, box, widget) {
return Switch(
value: box.get('darkMode'),
onChanged: (val) {
box.put('darkMode', val);
}
);
},
);
}
}
我正在构建一个独立的(没有 API 调用 MongoDB 或 Firebase 等)数据库管理至关重要的 flutter 应用程序。目前,我将其存储为 JSON,但效率很低。我不能使用 SQLite,因为数据非常嵌套,事实上,JSON 是存储我的数据的唯一方法。所以,我正在寻找 NoSQL 替代方案。
这是我要存储的数据模型。
lib/book_model.dart
@JsonSerializable()
class Book {
/// Do not change this. It's a primary key.
String bookLink;
String authors = null;
String thumbnail = null;
String bookName = null;
List<Chapter> totalChaptersList = [];
///todo: determine datatype
var currentChapter;
String summary = null;
double rating = 0.0;
List<String> genres = [];
Book({
this.bookLink,
this.authors,
this.thumbnail,
this.bookName,
this.totalChaptersList,
this.currentChapter,
this.summary,
this.rating,
this.genres,
});
Book.generateFromSearchBook(SearchBook searchBook) {
this.authors = searchBook.authors;
this.bookLink = searchBook.bookLink;
this.bookName = searchBook.bookName;
this.thumbnail = searchBook.thumbnail;
}
// @override
// String toString() {
// return "<$bookLink , $authors , $thumbnail , $bookName , $summary , $genres , $rating , $totalChaptersList , $currentChapter>";
// }
factory Book.fromJson(Map<String, dynamic> json) => _$BookFromJson(json);
Map<String, dynamic> toJson() => _$BookToJson(this);
}
@JsonSerializable()
class Chapter {
String name = null;
String date = null;
String chapterLink = null;
@JsonKey(defaultValue: false)
bool has_read = false;
List<Page> pages = [];
Chapter({
this.name,
this.date,
this.chapterLink,
this.has_read,
this.pages,
});
// @override
// String toString() {
// return "<$name , $date , $chapterLink , $has_read , $pages>";
// }
factory Chapter.fromJson(Map<String, dynamic> json) => _$ChapterFromJson(json);
Map<String, dynamic> toJson() => _$ChapterToJson(this);
}
@JsonSerializable()
class Page {
String pageLink = null;
int pageNumber = 0;
Page({
this.pageLink,
this.pageNumber,
});
// @override
// String toString() {
// return "<$pageLink , $pageNumber>";
// }
factory Page.fromJson(Map<String, dynamic> json) => _$PageFromJson(json);
Map<String, dynamic> toJson() => _$PageToJson(this);
}
我找到了这两个
我使用 Hive. It's fast and easy to use.you can use hive_generator 与 json_serializable 和 freezed 创建一个 HiveObject class.
示例:
@HiveType(typeId: 0)
class Person extends HiveObject {
@HiveField(0)
String name;
@HiveField(1)
int age;
}
您还可以使用 hive_box 将您的蜂巢箱变成可监听的并监听数据库变化。
示例:
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: Hive.box('settings').listenable(),
builder: (context, box, widget) {
return Switch(
value: box.get('darkMode'),
onChanged: (val) {
box.put('darkMode', val);
}
);
},
);
}
}