我很陌生,所以让我知道是否有人需要更多信息。 我一直在尝试在我的flutter应用程序中使用Hive。我想为我编写的称为Product的类生成自定义适配器。当我运行“ flutter包pub run build_runner build”时,这就是我的输出:
NoSuchMethodError: The getter 'element' was called on null.
Receiver: null
Tried calling: element
[SEVERE] hive_generator:hive_generator on lib/Pages/Content Cards/productCard.dart:
NoSuchMethodError: The getter 'element' was called on null.
Receiver: null
Tried calling: element
[SEVERE] hive_generator:hive_generator on lib/Pages/Content Cards/featuredCard.dart:
NoSuchMethodError: The getter 'element' was called on null.
Receiver: null
Tried calling: element
[INFO] 19.8s elapsed, 5/14 actions completed.
[WARNING] hive_generator:hive_generator on lib/Models/product.dart:
Missing "part 'product.g.dart';".
[INFO] Running build completed, took 20.2s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 42ms
[SEVERE] Failed after 20.2s
pub finished with exit code 1
这是产品类:https://pastebin.com/0a0W18ep
这是productCard.dart:https://pastebin.com/gV7kP2YG
这是FeaturedCard.dart:https://pastebin.com/7tke6V17
这是我pubsec.yaml的相关部分:
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: ^0.1.3
http: ^0.12.2
hive: ^1.4.4
hive_flutter: ^0.3.1
# For OS-specific directory paths
path_provider: ^1.6.18
dev_dependencies:
build_runner:
flutter_test:
sdk: flutter
hive_generator:
dependency_overrides:
analyzer:
我尝试过更改build_runner和分析器的版本,并在从中提取值之前检查产品对象是否为null。不幸的是没有一个工作。
答案 0 :(得分:0)
错误消息表明您是Missing "part 'product.g.dart';".
。
我检查了您的代码,但它丢失了。在product.dart
中添加此缺失的语句。
import 'package:hive/hive.dart';
part 'product.g.dart';
@HiveType(typeId: 1)
class Product {
/// other code ...
}