I was using build_runner and json_serializable to bind my json model,but get a error

This is my code structure:

lib/models

My user.dart was:

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User extends Object {

  @JsonKey(name: 'login')
  String login;

  @JsonKey(name: 'avatar_url')
  String avatarUrl;

  @JsonKey(name: 'type')
  String type;

  @JsonKey(name: 'name')
  String name;

  @JsonKey(name: 'company')
  String company;

  @JsonKey(name: 'blog')
  String blog;

  @JsonKey(name: 'location')
  String location;

  @JsonKey(name: 'email')
  String email;

  @JsonKey(name: 'hireable')
  bool hireable;

  @JsonKey(name: 'bio')
  String bio;

  @JsonKey(name: 'public_repos')
  int publicRepos;

  @JsonKey(name: 'followers')
  int followers;

  @JsonKey(name: 'following')
  int following;

  @JsonKey(name: 'created_at')
  String createdAt;

  @JsonKey(name: 'updated_at')
  String updatedAt;

  @JsonKey(name: 'total_private_repos')
  int totalPrivateRepos;

  @JsonKey(name: 'owned_private_repos')
  int ownedPrivateRepos;

  User(this.login,this.avatarUrl,this.type,this.name,this.company,this.blog,this.location,this.email,this.hireable,this.bio,this.publicRepos,this.followers,this.following,this.createdAt,this.updatedAt,this.totalPrivateRepos,this.ownedPrivateRepos,);

  factory User.fromJson(Map<String, dynamic> srcJson) => _$UserFromJson(srcJson);

  Map<String, dynamic> toJson() => _$UserToJson(this);

}

After command run flutter packages pub run build_runner watch ,I get this user.g.dart:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'user.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

User _$UserFromJson(Map<String, dynamic> json) {
  return User(
    json['login'] as String,
    json['avatar_url'] as String,
    json['type'] as String,
    json['name'] as String,
    json['company'] as String,
    json['blog'] as String,
    json['location'] as String,
    json['email'] as String,
    json['hireable'] as bool,
    json['bio'] as String,
    json['public_repos'] as int,
    json['followers'] as int,
    json['following'] as int,
    json['created_at'] as String,
    json['updated_at'] as String,
    json['total_private_repos'] as int,
    json['owned_private_repos'] as int,
  );
}

Map<String, dynamic> _$UserToJson(User instance) => <String, dynamic>{
      'login': instance.login,
      'avatar_url': instance.avatarUrl,
      'type': instance.type,
      'name': instance.name,
      'company': instance.company,
      'blog': instance.blog,
      'location': instance.location,
      'email': instance.email,
      'hireable': instance.hireable,
      'bio': instance.bio,
      'public_repos': instance.publicRepos,
      'followers': instance.followers,
      'following': instance.following,
      'created_at': instance.createdAt,
      'updated_at': instance.updatedAt,
      'total_private_repos': instance.totalPrivateRepos,
      'owned_private_repos': instance.ownedPrivateRepos,
    };

But the dart analysis show:

enter image description here

The error only happened on my user.part,other .part(like profille.part,repo.part)not. I wanna know why this error occured


Solution 1: The Flying Dutchman

Same thing happened to me. Here is how I fixed it:

I copied the code of user.dart into another text editor and completely deleted user.dart and user.g.dart

then I created a new user.dart and pasted the contents back. And then I ran flutter packages pub run build_runner watch and it fixed the issue.

I think what happened is that you (and I) accidentally created "User.dart" instead of "user.dart", and VSCode cached this somewhere and looked it up from the cache. But that is just my guess.


Solution 2: Mittal Shubham

For me, I had to go to the generated .g.dart file, and then there correct the part of statement to refer to the correct file.


Solution 3: flashrow

Another solution for this problem, that I've found is closing VSCode, then deleting cache directories from your's project repository(like .dart_tool, .vscode) and running the build_runner again.


Solution 4: Mr B

Try to rename that file to another name and rename that file name back to the original name.

I am able to solve this issue via above method.


Solution 5: DroidBoyJr

Just closing and reopening vs code did the trick for me.