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

1.8k views Asked by At

I'm pretty new to flutter so let me know if anyone needs more info. I've been trying to use Hive with my flutter app. I want to generate a custom adapter for a class I wrote called Product. When I run "flutter packages pub run build_runner build", this is what I get as output:

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

This is the Product class: https://pastebin.com/0a0W18ep

This is productCard.dart: https://pastebin.com/gV7kP2YG

This is featuredCard.dart: https://pastebin.com/7tke6V17

Here is the relevant part of my 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:

I've tried altering the versions of the build_runner and analyzer and checking if a product object is null before extracting values from it. None have worked unfortunately.

1

There are 1 answers

5
Tirth Patel On BEST ANSWER

The error message says that you're Missing "part 'product.g.dart';"..

I checked your code and it's missing that. Add this missing statement in product.dart.

import 'package:hive/hive.dart';
 
part 'product.g.dart';

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