@freezed
abstract class Person extends HiveObject with _$Person {
@HiveType(typeId: 0)
factory Person({@HiveField(0) String name, @HiveField(1) int age}) = _Person;
}
Run Code Online (Sandbox Code Playgroud)
我知道这是不可能的,但我想你知道我想要实现什么。用蜂巢冻结的最佳方法是什么?
我目前能想到的唯一解决方案是存储在 hive 中冻结生成的 json-String。但我希望有更好的解决方案。
我已经用谷歌搜索了这个错误,但是我得到的答案对我没有帮助。我正在尝试使用 Vuex 进行喜欢/不喜欢计数。我正在使用 jsonplaceholder 来获取数据。在这里,我获取数据并为所有对象设置类似的属性。
actions: {
async fetchPhotos({commit}) {
const response = await axios.get('https://jsonplaceholder.typicode.com/photos');
commit('setPhotos', response.data.splice(0,100))
},
}
mutations: {
setPhotos: (state, photos) => {
(state.albumList = photos);
state.albumList.forEach(element => element.likes = 0)}
}
Run Code Online (Sandbox Code Playgroud)
在我的 dom 之后,我想使用一个按钮表示不喜欢,使用另一个按钮表示喜欢。我想在单击按钮时显示类似计数器。
<div v-for="photos in allPhotos" :key="photos.id">
<div class="card" @click="detail(photos)"><img :src="photos.thumbnailUrl" alt=""></div>
<div class="likes">
<div class="myButton" ><button class="red" @click="minus(photos.id)"><i class="fas fa-minus"></i></button></div>
<div>{{photos.likes}}</div>
<div class="myButton "><button class="green" @click="plus(photos.id)"><i class="fas fa-plus"></i></button></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
所以我在我的商店和脚本中添加了减突变。
methods: {
...mapMutations(['minus']),
Run Code Online (Sandbox Code Playgroud)
state: {
albumList : [],
},
mutations: {
minus: …
Run Code Online (Sandbox Code Playgroud) 我运行flutter packages pug run build_runner build
在 Flutter 中创建 TypeAdapter 的普通命令,但出现以下错误:
类型“UnspecifiedInvalidResult”不是类型转换中类型“LibraryElementResult”的子类型
它说它在以下文件中发现了错误:
import 'dart:core';
import 'package:hive/hive.dart';
part 'storedItem.g.dart';
@HiveType(typeId: 1)
class Person extends HiveObject {
@HiveField(0)
String name;
@HiveField(1)
int age;
Person({required this.name, required this.age});
}
Run Code Online (Sandbox Code Playgroud)
我的意思是这就是文档的代码!我做错了什么?
顺便说一句:在 M1 MacBook Air、Flutter 2.2.3、Android Studio 4.2.2 上进行开发
我的模型类中有一个枚举:
MyRepresentation { list, tabs, single }
Run Code Online (Sandbox Code Playgroud)
我已经添加了一个适配器并注册了它。我已经给它一个正确的类型 ID 和字段。
它给出错误:
HiveError:无法写入,未知类型:MyRepresentation。您是否忘记注册适配器?
我有一个墙纸应用程序,它使用 Firestore 来存储墙纸。
我想使用 Hive 来存储来自 cloud firestore 的壁纸列表,但是如何保存壁纸列表并在以后检索它?
当我尝试保存列表时,出现此错误:
E/flutter (9995): [ERROR:flutter/shell/common/shell.cc(199)] Dart 错误:未处理的异常:E/flutter (9995):HiveError:无法写入,未知类型:墙纸。您是否忘记注册适配器?
代码:
class Wallpaper extends HiveObject {
String date;
String url;
Wallpaper();
}
static Future<void> addWallpapers({@required String boxName, @required List<Wallpaper> wallpapers}) async {
var box = await Hive.openBox(boxName);
box.put(boxName, wallpapers);
print("WALLPAPER ADICIONADO NO HIVE!");
}
static Future<List<Wallpaper>> getWallpapers({@required String boxName}) async {
var box = await Hive.openBox(boxName);
List<Wallpaper> wallpapers = box.get("latest");
return wallpapers;
}
Run Code Online (Sandbox Code Playgroud) 我目前正在Hive
我的 FlutterApp 中实现。不幸的是这个错误总是弹出:
HiveError:已经有一个 typeId 100 的 TypeAdapter。
这是我的对象:
@HiveType(typeId: 100)
class ShopList{
@HiveField(0)
String name;
@HiveField(1)
List<ListProduct> products = List();
ShopList({this.name, this.products});
Run Code Online (Sandbox Code Playgroud)
这是自动生成的适配器:
class ShopListAdapter extends TypeAdapter<ShopList> {
@override
final int typeId = 100;
@override
ShopList read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return ShopList(
name: fields[0] as String,
products: (fields[1] as List)?.cast<ListProduct>(),
);
}
@override
void write(BinaryWriter writer, ShopList …
Run Code Online (Sandbox Code Playgroud) 我使用Hive Packages,我在我的项目中实现了模块化。首先,我network
使用运行命令创建名称为包的包flutter create --template=package network
,我引用了这个。
这个包包括我的项目模型。之后我创建模型用户,然后运行命令构建模型flutter packages pub run build_runner build --delete-conflicting-outputs
:
import 'package:hive/hive.dart';
part 'user_model_hive.g.dart';
@HiveType()
class UserModelHive extends HiveObject {
@HiveField(0)
DateTime id;
@HiveField(1)
String giverName;
@HiveField(2)
String pinCodeNumber;
UserModelHive({this.id, this.giverName, this.pinCodeNumber});
}
Run Code Online (Sandbox Code Playgroud)
但我得到这样的错误
Could not find package "build_runner". Did you forget to add a dependency? pub finished with exit code 65
我确定已经包含build_runner
在我的包中network
。
pubspec.yaml
name: network
description: A new Flutter package project.
version: 0.0.1 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试访问一些在整个应用程序中更改和访问的用户设置。为此,我使用 Hive 从框中检索用户设置。我想通过将检索到的对象转换为UserSettings
将Hive.box('settings').get(userID)
被输入到StreamProvider
.
我将如何变成Hive.box('settings').get(userID)
一个流?
编辑:这是迄今为止我的代码实现:
本地数据库.dart
static Stream<UserSettings> get userSettings {
return Hive.box('settings').watch(key: userID)?.map((boxEvent) {
return boxEvent.value; // ========> value is always null from boxEvent
});
}
static void saveUserSettings(UserSettings userPref) {
Hive.box('settings').put(userID, userPref);
}
Run Code Online (Sandbox Code Playgroud)
主程序.dart
class Launch extends StatefulWidget {
@override
_LaunchState createState() => _LaunchState();
}
class _LaunchState extends State<Launch> {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
StreamProvider<User>.value(value: AuthService.user),
StreamProvider<UserSettings>.value(value: LocalDatabase.userSettings),
],
child: MaterialApp(
title: 'MyApp',
home: AuthWrapper(), …
Run Code Online (Sandbox Code Playgroud) 我的问题是 flutter Hive 无法从多个隔离区打开。因此,我希望将 workmananger 任务中获取的数据发送到打开配置单元框的主隔离区,并在那里对其进行修改以避免损坏它。我应该考虑应用程序何时处于活动状态以及何时不存在 => 例如,当应用程序不处于活动状态时,我直接编辑配置单元文件,因为它只会在 workmanager 隔离中打开,而如果应用程序不处于活动状态,我会直接编辑配置单元文件将数据发送到主隔离并编辑配置单元文件。我的问题是,我不知道如何跟踪工作管理器任务中的生命周期,也不知道如何将数据发送到主隔离。该问题有任何解决方法或解决方案,或者如何对上述内容进行编码?
谁能告诉我如何在 Flutter 中存储和检索 Hive 数据库中的图像?
我想上传图像,然后使用配置单元在其他页面中显示图像,我尝试过,但无法做到这一点。
dart ×9
flutter ×9
flutter-hive ×9
asynchronous ×1
build-runner ×1
casting ×1
javascript ×1
vue.js ×1