当使用 build_runner 时,得到 "nosuchmethoderror: getter 'element' was called on null."

ee7vknir  于 2021-04-03  发布在  Hive
关注(0)|答案(1)|浏览(639)

我对flutter很陌生,所以如果有人需要更多的信息,请告诉我。我一直在尝试使用hive与我的flutter应用程序。我想为我写的一个叫做product的类生成一个自定义适配器。

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
这是特色卡.镖: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和analyzer的版本,并在从中提取值之前检查产品对象是否为空。

zbq4xfa0

zbq4xfa01#

错误信息说你 "缺少 "part 'product.g.dart';".。 我检查了你的代码,它是缺少的。添加这个缺失的语句在product.dart`。

import 'package:hive/hive.dart';

part 'product.g.dart';

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

相关问题