Json_serializable.dart: Error while generating code in package, but works OK when in app (The getter 'type' isn't defined for the class 'Element')

Created on 14 Aug 2019  Â·  11Comments  Â·  Source: google/json_serializable.dart

I have several classes that can be generated flawlessly when placed in main app directory but generation fails when placed in package. Of course I run flutter pub run build_runner build --define "json_serializable=any_map=true" --verbose in respective directories i.e. in app or in package.

Both app and package have the same dependencies in pubspec.yaml.

Package was generated today with command: flutter create --template=package package

Already tried: flutter clean, removing .dart_tool, build_runner clean.

Output of flutter pub run build_runner build --define "json_serializable=any_map=true" --verbose:

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

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 7.3s

[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:40:64: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.    final checkerA = TypeChecker.fromStatic(a.enclosingElement.type);                                                               ^^^^file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:50:66: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.      final checkerB = TypeChecker.fromStatic(b.enclosingElement.type);                                                                 ^^^^
pub failed (78)

#0      throwToolExit (package:flutter_tools/src/base/common.dart:28:3)
#1      pub (package:flutter_tools/src/dart/pub.dart:173:5)
<asynchronous suspension>
#2      PackagesForwardCommand.runCommand (package:flutter_tools/src/commands/packages.dart:196:11)
<asynchronous suspension>
#3      FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:478:18)
<asynchronous suspension>
#4      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:383:33)
<asynchronous suspension>
#5      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:29)
<asynchronous suspension>
#6      _rootRun (dart:async/zone.dart:1124:13)
#7      _CustomZone.run (dart:async/zone.dart:1021:19)
#8      _runZoned (dart:async/zone.dart:1516:10)
#9      runZoned (dart:async/zone.dart:1463:12)
#10     AppContext.run (package:flutter_tools/src/base/context.dart:152:18)
<asynchronous suspension>
#11     FlutterCommand.run (package:flutter_tools/src/runner/flutter_command.dart:375:20)
#12     CommandRunner.runCommand (package:args/command_runner.dart:197:27)
<asynchronous suspension>
#13     FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:396:21)
<asynchronous suspension>
#14     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:29)
<asynchronous suspension>
#15     _rootRun (dart:async/zone.dart:1124:13)
#16     _CustomZone.run (dart:async/zone.dart:1021:19)
#17     _runZoned (dart:async/zone.dart:1516:10)
#18     runZoned (dart:async/zone.dart:1463:12)
#19     AppContext.run (package:flutter_tools/src/base/context.dart:152:18)
<asynchronous suspension>
#20     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:356:19)
<asynchronous suspension>
#21     CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:112:25)
#22     new Future.sync (dart:async/future.dart:224:31)
#23     CommandRunner.run (package:args/command_runner.dart:112:14)
#24     FlutterCommandRunner.run (package:flutter_tools/src/runner/flutter_command_runner.dart:242:18)
#25     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:62:22)
<asynchronous suspension>
#26     _rootRun (dart:async/zone.dart:1124:13)
#27     _CustomZone.run (dart:async/zone.dart:1021:19)
#28     _runZoned (dart:async/zone.dart:1516:10)
#29     runZoned (dart:async/zone.dart:1500:12)
#30     run.<anonymous closure> (package:flutter_tools/runner.dart:60:18)
<asynchronous suspension>
#31     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:29)
<asynchronous suspension>
#32     _rootRun (dart:async/zone.dart:1124:13)
#33     _CustomZone.run (dart:async/zone.dart:1021:19)
#34     _runZoned (dart:async/zone.dart:1516:10)
#35     runZoned (dart:async/zone.dart:1463:12)
#36     AppContext.run (package:flutter_tools/src/base/context.dart:152:18)
<asynchronous suspension>
#37     runInContext (package:flutter_tools/src/context_runner.dart:56:24)
<asynchronous suspension>
#38     run (package:flutter_tools/runner.dart:51:10)
#39     main (package:flutter_tools/executable.dart:62:9)
<asynchronous suspension>
#40     main (file:///Users/dominik/Library/flutter/packages/flutter_tools/bin/flutter_tools.dart:8:3)
#41     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#42     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

I have following structure of the app:

- app
    - lib
        - model
            - file.dart <- works OK when placed here
            - file.g.dart
- package
    - lib
        - model
            - file.dart <- fails when placed here
            - file.g.dart <- it's not generated

Flutter version:

Flutter 1.7.8+hotfix.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 20e59316b8 (4 weeks ago) • 2019-07-18 20:04:33 -0700
Engine • revision fee001c93f
Tools • Dart 2.4.0

Output of flutter packages pub run build_runner clean

[WARNING] Deleting cache and generated source files.
This shouldn't be necessary for most applications, unless you have made intentional edits to generated files (i.e. for testing). Consider filing a bug at https://github.com/dart-lang/build/issues/new if you are using this to work around an apparent (and reproducible) bug.
[INFO] Cleaning up source outputs...
[WARNING] No asset graph found. Skipping cleanup of generated files in source directories.
[INFO] Cleaning up source outputs completed, took 4ms

[INFO] Cleaning up cache directory...
[INFO] Cleaning up cache directory completed, took 4ms

Output of: flutter packages pub run build_runner build --delete-conflicting-outputs

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

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 7.4s

[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:40:64: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.    final checkerA = TypeChecker.fromStatic(a.enclosingElement.type);                                                               ^^^^file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:50:66: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/dominik/Library/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.      final checkerB = TypeChecker.fromStatic(b.enclosingElement.type);                                                                 ^^^^
pub finished with exit code 78

My solution now is to generate files in app directory and then copy them to package. Do you know what may be wrong here?

Most helpful comment

my sample model

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  final int id;
  final String name;
  final String username;
  final String email;

  User(this.id, this.name, this.username, this.email);

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

my output

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

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 6.7s

[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:40:64: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.    final checkerA = TypeChecker.fromStatic(a.enclosingElement.type);                                                               ^^^^file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:50:66: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.      final checkerB = TypeChecker.fromStatic(b.enclosingElement.type);                                                                 ^^^^
pub failed (78)

All 11 comments

I also have such a problem and cannot find a solution

Same

my sample model

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  final int id;
  final String name;
  final String username;
  final String email;

  User(this.id, this.name, this.username, this.email);

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

my output

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

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 6.7s

[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:40:64: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.    final checkerA = TypeChecker.fromStatic(a.enclosingElement.type);                                                               ^^^^file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/json_serializable-3.2.0/lib/src/field_helpers.dart:50:66: Error: The getter 'type' isn't defined for the class 'Element'. - 'Element' is from 'package:analyzer/dart/element/element.dart' ('file:///Users/sergejozeranskij/development/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.1/lib/dart/element/element.dart').Try correcting the name to the name of an existing getter, or defining a getter or field named 'type'.      final checkerB = TypeChecker.fromStatic(b.enclosingElement.type);                                                                 ^^^^
pub failed (78)

solved the problem by installing the previous version analyzer:

dependencies:
  analyzer: 0.37.0

solved the problem by installing the previous version analyzer:

dependencies:
  analyzer: 0.37.0

Thank you, adding analyzer without the version solved my problem.

FYI analyzer 0.37.1+1 was just released that should also address this!

See https://github.com/dart-lang/sdk/issues/37854

I love you guys!

The only solution that worked for me was to delete manually all *.g.dart files and then run flutter pub run build_runner build.

In my case output looks like this, even if i add analyzer package in my pubspec.yaml file.

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

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 7.5s

[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] ../../sdk/flutter/.pub-cache/hosted/pub.dartlang.org/retrofit_generator-1.1.0/lib/src/generator.dart:700:39: Error: The method 'getDisplayString' isn't defined for the class 'DartType'. - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../sdk/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.37.0/lib/dart/element/type.dart').Try correcting the name to the name of an existing method, or defining a method named 'getDisplayString'.      return refer(constant.typeValue.getDisplayString());   

@darshakpranpariya – it seems you don't have the right version of pkg:analyzer

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexBigCheese picture AlexBigCheese  Â·  3Comments

Nico04 picture Nico04  Â·  5Comments

matanlurey picture matanlurey  Â·  5Comments

ollydixon picture ollydixon  Â·  6Comments

kevmoo picture kevmoo  Â·  3Comments