我希望使用 Flutter 和 ObjectBox (1.1.1 -> Flutter) 定义最佳开发实践。
我想建立一个由Repository组成的架构来实现对数据库中存储的对象的查询功能。这是一个对象人的例子。存储库主要从 viewModel 调用。
import 'package:flutter_app_best_practice/data/person_repository_interface.dart';
import 'package:flutter_app_best_practice/model/person.dart';
import 'package:flutter_app_best_practice/objectbox.g.dart';
import 'package:objectbox/objectbox.dart';
class PersonRepository implements IPersonRepository{
var _store;
late Box _box;
@override
Future<List<Person>>? getPeople() {
List<Person> list = _box.getAll() as List<Person>;
return Future.value(list);
}
@override
Future<void>? addPerson(String name, int age) async {
Person person = new Person();
person.name = name;
person.age = age;
_box.put(person); // Add
return;
}
@override
Future<void>? initialize() async {
// Future<Store> openStore() {...} is defined in the generated objectbox.g.dart …Run Code Online (Sandbox Code Playgroud) 我正在使用对象框版本1.1.0.使用ObjectBox文档中给出的说明进行安装,但收到此错误.即使我在Objectbox的FAQ部分搜索了这个问题,他们提供的解决方案是删除android-apt.但是在gradle文件中没有使用android-apt.那么,如何克服这个问题请帮助我.Bellow是我的根级gradle文件
buildscript {
ext {
_buildToolsVersion = '26.0.1'
_compileSdkVersion = 26
objectboxVersion = '1.1.0'
}
repositories {
jcenter()
mavenCentral()
maven { url "http://objectbox.net/beta-repo/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.1.0'
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
}
}allprojects {
repositories {
jcenter()
mavenCentral()
maven {
url 'https://maven.fabric.io/public'
}
maven { url "http://objectbox.net/beta-repo/"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
app gradle文件如下
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.kingof.android"
minSdkVersion 16
targetSdkVersion 25
versionCode 3
versionName …Run Code Online (Sandbox Code Playgroud) 我正在 flutter 中构建聊天应用程序。
我找到了带有 dart 的 objectbox。看起来不错。之前我尝试过蜂巢。
我的问题是。Objectbox是内存数据库还是磁盘数据库?
当我打开应用程序时,Hive 位于内存数据库中。它将所有盒子数据加载到内存中。但我正在寻找像磁盘存储这样的解决方案。这样我就可以在需要时将数据加载到内存中。对象框如何工作?
我有一个带有 Map 属性的类,但它似乎不存储该属性。所有其他属性都存储良好:
@JsonSerializable()
@Entity()
class PossessionStats {
@JsonKey(defaultValue: 0)
int id;
String cardUuid;
int total;
Map<String, int>? totalByVersion;
CardPossessionStats({
this.id = 0,
required this.cardUuid,
this.total = 0,
this.totalByVersion,
});
}
Run Code Online (Sandbox Code Playgroud)
当我保存一个时:
stats = PossessionStats(cardUuid: card.uuid);
if (stats.totalByVersion == null) {
stats.totalByVersion = Map();
}
stats.totalByVersion!
.update(version, (value) => quantity, ifAbsent: () => quantity);
stats.total = stats.totalByVersion!.values
.fold(0, (previousValue, element) => previousValue + element);
box.put(stats);
Run Code Online (Sandbox Code Playgroud)
当我得到结果时(刷新后),我可以看到total是正确的,但totalByVersion仍然是空的。
谢谢!