Getting error while adding entity in flutter project
Asked Answered
D

1

6

I had added multiple entities in object box and successfully build the build_runner. flutter pun run build_runner build. Now, I am adding two more entities but I am getting this error - could not resolve annotation @Entity()

Trying to add entities

import 'package:formula_application/objectbox.g.dart';
import 'package:json_annotation/json_annotation.dart';

part 'stop_test_model.g.dart';

@JsonSerializable(
  explicitToJson: true,
  fieldRename: FieldRename.pascal,
)
@Entity()
class StopTest {
  StopTest(this.id, this.name);
  int id = 0;
  String name;
  //@Backlink()
  //final routes = ToMany<RouteTest>();

  factory StopTest.fromJson(Map<String, dynamic> json) =>
      _$StopTestFromJson(json);
  Map<String, dynamic> toJson() => _$StopTestToJson(this);
}

\\separate route test file

import 'package:formula_application/objectbox.g.dart';
import 'package:json_annotation/json_annotation.dart';

part 'route_test_model.g.dart';

@JsonSerializable(
  explicitToJson: true,
  fieldRename: FieldRename.pascal,
)
@Entity()
class RouteTest {
  RouteTest(this.id, this.name);
  int id = 0;
  String name;
  //final stops = ToMany<StopTest>();

  factory RouteTest.fromJson(Map<String, dynamic> json) =>
      _$RouteTestFromJson(json);
  Map<String, dynamic> toJson() => _$RouteTestToJson(this);
}

After build_runner build command, I am getting this error

[INFO] Generating build script... [INFO] Generating build script completed, took 514ms

[INFO] Initializing inputs [INFO] Reading cached asset graph... [INFO] Reading cached asset graph completed, took 138ms

[INFO] Checking for updates since last build... [INFO] Checking for updates since last build completed, took 926ms

[INFO] Running build... [SEVERE] json_serializable:json_serializable on lib/models/test_model/route_test_model.dart:

Null check operator used on a null value [SEVERE] json_serializable:json_serializable on lib/models/test_model/stop_test_model.dart:

Null check operator used on a null value [SEVERE] objectbox_generator:resolver on lib/models/test_model/stop_test_model.dart:

line 1, column 225 of package:formula_application/models/test_model/stop_test_model.dart: Could not resolve annotation for class StopTest. ╷ 1 │ @Entity()
│ ^^^^^^^^^ ╵ [WARNING] objectbox_generator:resolver on lib/models/test_model/route_test_model.dart: An unexpected error was thrown trying to get location information on class RouteTest (ClassElementImpl).

Please file an issue at https://github.com/dart-lang/source_gen/issues/new Include the contents of this warning and the stack trace along with the version of package:source_gen, package:analyzer from pubspec.lock.

InconsistentAnalysisException: Requested result might be inconsistent with previously returned results

[SEVERE] objectbox_generator:resolver on lib/models/test_model/route_test_model.dart:

Could not resolve annotation for class RouteTest. [INFO] 4.2s elapsed, 4/6 actions completed. [INFO] Running build completed, took 4.5s

[INFO] Caching finalized dependency graph... [INFO] Caching finalized dependency graph completed, took 66ms

[SEVERE] Failed after 4.6s pub finished with exit code 1

I have tried to run the same without @JsonSerializable() annotation, using the plain class. Still getting same error.

Disturb answered 26/7, 2021 at 9:7 Comment(4)
Can you paste the whole file (including imports)?Krishnakrishnah
It's rather weird though, especially since the error doesn't happen in objectbox-generator, rather in Dart's source_gen package. A minimal reproducible example could help to get you the answer.Krishnakrishnah
@Krishnakrishnah thanks for pointing towards the error, while typing "@Entity()" the objectbox.g.dart file was automatically imported and I didn't check that. Now, when I was checking the imports, I changed the import from objectbox.g.dart to 'package:objectbox/objectbox.dart' and build is working fine nowDisturb
@Disturb thanks for posting that solution. That was the same issue for me. Spent way too long trying to figure that out.Hettiehetty
P
6

Change the

import 'package:formula_application/objectbox.g.dart';

to

import 'package:objectbox/objectbox.dart';
Psilomelane answered 17/1, 2023 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.