Getting "NoSuchMethodError: The getter 'element' was called on null." when using build_runner

Getting "NoSuchMethodError: The getter 'element' was called on null." when using build_runner

我是 Flutter 的新手,所以如果有人需要更多信息,请告诉我。 我一直在尝试将 Hive 与我的 flutter 应用程序一起使用。我想为我编写的名为 Product 的 class 生成自定义适配器。当我 运行 “flutter packages pub 运行 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

这是产品 class: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 和分析器的版本,并在从中提取值之前检查产品对象是否为空。 None 不幸工作了。

错误消息说您是 Missing "part 'product.g.dart';".

我检查了您的代码,但没有找到。在 product.dart.

中添加此缺失语句
import 'package:hive/hive.dart';
 
part 'product.g.dart';

@HiveType(typeId: 1)
class Product {
   /// other code ...
}