Flutter hive:无法生成适配器文件
Posted
技术标签:
【中文标题】Flutter hive:无法生成适配器文件【英文标题】:Flutter hive: cannot generate Adapter files 【发布时间】:2021-11-06 23:10:19 【问题描述】:我有一个这样的模型:
import 'package:uuid/uuid.dart';
import 'package:hive/hive.dart';
part 'config_item.g.dart';
@HiveType()
class ConfigItem
@HiveField(0)
String _id; // this can be a uuid or a MongoDB ObjectID
@HiveField(1)
final String deviceName;
....
我喜欢生成适配器文件,但它不想这样做!当我调用flutter packages pub run build_runner build --delete-conflicting-outputs
时,我得到以下输出:
flutter packages pub run build_runner build --delete-conflicting-outputs main ✭ ✈
[INFO] Generating build script...
[INFO] Generating build script completed, took 399ms
[SEVERE] Nothing can be built, yet a build was requested.
[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 45ms
[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 399ms
[INFO] Running build...
[INFO] Running build completed, took 3ms
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 35ms
[INFO] Succeeded after 53ms with 0 outputs (0 actions)
在我的pubspec.yaml
我有:
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for ios style icons.
cupertino_icons: ^1.0.2
bottom_navy_bar: ^6.0.0
get_it: ^7.2.0
get_it_mixin: ^3.1.3
servicestack: ^2.0.0
font_awesome_flutter: ^9.1.0
hive: ^2.0.4
hive_flutter: ^1.1.0
path_provider: ^2.0.3
uuid: ^3.0.4
dev_dependencies:
flutter_test:
sdk: flutter
build_runner:
我尝试了各种方法,例如
删除 .dart_tool 文件夹 将*.g.dart
添加到.gitignore
flutter clean
还有很多我在网上搜索时发现的。
但没有任何帮助!知道缺少什么吗?
我正在使用最新(稳定)版本的 Flutter、dart SDK 和 androidStudio。
【问题讨论】:
我认为您在开发依赖项中缺少hive_generator: ^1.1.1
。
这就是build
的解决方案。我必须在 @HiveType 注释中分配一个 typeId,如下所示:@HiveType(typeId : 1)
。非常感谢您的提示。
【参考方案1】:
运行这个命令:
flutter packages pub run build_runner build
但在此之前,您必须导入生成器。
示例:如果您的文件名为 project_database.dart,则在该文件中:
导入,
import 'package:hive/hive.dart';
part 'project_database.g.dart'; //this will show an error initially but if
// you run the above command, it will generate the generator file
【讨论】:
【参考方案2】:我有同样的问题。我通过将更改保存在 pubspec.yaml 中来解决。 只需在 pubspec.yaml 中按 ctrl + s 然后运行 flutter packages pub run build_runner build 再次。
【讨论】:
【参考方案3】:我遇到了同样的问题,但我错过了hive_generator: ^1.1.1
。原来Docs里没找到。
特别感谢@Denny Mueller 的评论
【讨论】:
以上是关于Flutter hive:无法生成适配器文件的主要内容,如果未能解决你的问题,请参考以下文章
用于 Geopoint 的 Flutter Hive 适配器
Hive 错误未知类型 TimeStamp 。您是不是忘记注册适配器。如何为外部包中使用的对象类型生成 TypeAdapter?