运行 flutter pub run build_runner build 时出现此错误。
Failed to build build_runner:build_runner:
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.41.2/lib/src/error/best_practices_verifier.dart:258:50: Error: The property 'displayString' is defined in multiple extensions for 'TargetKind' and neither is more specific.
- 'TargetKind' is from 'package:meta/meta_meta.dart' ('../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.7.0/lib/meta_meta.dart').
Try using an explicit extension application of the wanted extension or hiding unwanted extensions from scope.
var kindNames = kinds.map((kind) => kind.displayString).toList()
^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.41.2/lib/src/error/best_practices_verifier.dart:1950:14: Context: This is one of the extension members.
String get displayString {
^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.7.0/lib/meta_meta.dart:91:14: Context: This is one of the extension members. …Run Code Online (Sandbox Code Playgroud) @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。但我希望有更好的解决方案。
我正在使用此功能来确保正确打开和关闭盒子,但我仍然收到HiveError: Box has already been closed.错误
static Future<Box<dynamic>> openIt() async {
var connectionBox = await Hive.openBox(hiveBox);
if (Hive.isBoxOpen(hiveBox) == false) {
await Hive.openBox(hiveBox);
} else {
await connectionBox.close();
connectionBox = await Hive.openBox(hiveBox);
}
return connectionBox;
}
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 - Package 在本地存储一些数据。到目前为止,一切都很好,但现在我面临一个问题:
我有一个Custom-Class其中也有一个字段与另一个Custom-Class:
part 'hive_vitals_interface.g.dart';
@HiveType(typeId: 1)
class HiveVitals extends HiveObject {
@HiveField(0)
String? id;
@HiveField(1)
DateTime? date;
@HiveField(2)
List<HiveDiscomfort> otherDiscomfort;
@HiveField(3)
List<HiveDiscomfort> mentalDiscomfort;
HiveVitals({
this.id,
this.date,
this.otherDiscomfort = const [],
this.mentalDiscomfort = const [],
});
}
Run Code Online (Sandbox Code Playgroud)
和我的HiveDiscomforts-Class:
part 'hive_discomfort_interface.g.dart';
@HiveType(typeId: 2)
class HiveDiscomfort extends HiveObject {
@HiveField(0)
String? title;
@HiveField(1)
int? intensity;
HiveDiscomfort({
this.title,
this.intensity,
});
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试这样保存: HiveVitals
static Future<void> addVitals(HiveVitals hiveVitals) async {
final vitalsBox = getVitalsBox(); …Run Code Online (Sandbox Code Playgroud) 我了解到,使用 flutter 中的 hive 数据库可以将自定义对象存储为 json 字符串。然后可以选择编写一个自定义类型适配器,以二进制格式存储数据。
那么,当开发人员只需以 json 字符串格式存储自定义对象时,为什么需要花费更多时间来实现类型适配器呢?
通过构建运行程序命令生成 .g.dart 文件时出现以下错误。您能帮忙找出这里出了什么问题吗?
flutter 包 pub run build_runner build
Unhandled exception:
Bad state: Unable to generate package graph, no `project/.dart_tool/flutter_gen/pubspec.yaml` found.
#0 _pubspecForPath (package:build_runner_core/src/package_graph/package_graph.dart:232:5)
#1 _parsePackageDependencies (package:build_runner_core/src/package_graph/package_graph.dart:206:21)
#2 PackageGraph.forPath (package:build_runner_core/src/package_graph/package_graph.dart:101:33)
<asynchronous suspension>
#3 main (file:///.../.pub-cache/hosted/pub.dartlang.org/build_runner-2.1.7/bin/build_runner.dart:27:30)
<asynchronous suspension>
pub finished with exit code 255
Run Code Online (Sandbox Code Playgroud)
这是我的 pubspec:
dependencies:
hive: 2.0.5
hive_flutter: 1.1.0
dev_dependencies:
hive_generator: 1.1.2
build_runner: 2.1.7
build_resolvers: 2.0.6
Run Code Online (Sandbox Code Playgroud)
我尝试清理颤振并再次重建。但是,它仍然有同样的错误。
有什么建议可以解决吗?
谢谢。