Boothosh81
Boothosh81

Reputation: 613

Error: "type 'UnspecifiedInvalidResult' is not a subtype of type 'LibraryElementResult' in type cast" in Flutter Hive

I run flutter packages pug run build_runner build, the normal command with which you would create a TypeAdapter in Flutter, I get the following error:

type 'UnspecifiedInvalidResult' is not a subtype of type 'LibraryElementResult' in type cast

It says that It founds the error In the following file:

import 'dart:core';
import 'package:hive/hive.dart';
part 'storedItem.g.dart';

@HiveType(typeId: 1)
class Person extends HiveObject {

  @HiveField(0)
  String name;

  @HiveField(1)
  int age;
  
  Person({required this.name, required this.age});
}

I mean that's the Code of the documentation! What did I wrong?

Btw: Developing on a M1 MacBook Air, Flutter 2.2.3, Android Studio 4.2.2

Upvotes: 9

Views: 3843

Answers (5)

Steps to fix the ploblem:

  • execute flutter clean.
  • deleted pubspec.lock.
  • run flutter pub get.

DONE

Now run the build_runner and shuld work

Upvotes: 0

Sadra
Sadra

Reputation: 106

I was able to fix this by deleting my pubspec.lock file, run flutter clean and running flutter pub get again.

Upvotes: 8

Code on the Rocks
Code on the Rocks

Reputation: 17614

I was finally able to fix this by deleting my pubspec.lock file and running flutter pub get again.

Upvotes: 18

Himanshu Thakur
Himanshu Thakur

Reputation: 61

I was facing the same problem right now. Finally figured out what I was doing wrong.

The solution is the name of the dart file should be used in part"file_name.g.dart"; and there should be no gaps in the file name. For example, my dart file name was Data Entry.dart and I was using part"entries.g.dart";. That's why it was showing this error. Then I changed the code to part"Data Entry.g.dart";. It was still showing the error.

Then finally I changed the file name to entries.dart and used part"entries.g.dart"; and it worked fine.

Upvotes: 5

Sn1cKa
Sn1cKa

Reputation: 601

source_gen: 1.0.3 helped in my case

Upvotes: 0

Related Questions