Json_serializable.dart: Does not generate .g.dart files any more

Created on 25 Mar 2019  路  3Comments  路  Source: google/json_serializable.dart

here is my classes and my user model that generated just one time, but it doesn't generate change after running build command and if i delete last user.g.dart it will not regenerate.

i have this warning some times:

WARNING] json_serializable on lib/model/auth/user.dart:
Missing "part 'user.g.dart';".

part 'package:haegisa/model/auth/user.g.dart';

///User model to store user information and some global properties
@JsonSerializable()
class User extends BaseModel<User> {

  String id;
  String name;
  AuthType authType = AuthType.none;
  String email;
  String imageUrl;

  @JsonKey(ignore: true)
  bool isSignedIn;

  User() : super('user-info') {
    //load user data from preferences
    Shared.load().then((shared) {
      id = Shared.getString(Shared.kUserId, def: '');
      name = Shared.getString(Shared.kUserName, def: '');
      isSignedIn = Shared.getBool(Shared.kIsSignedIn, def: false);
      authType = AuthType.values[Shared.getInt(Shared.kAuthType, def: AuthType.none.index)];
      email = Shared.getString(Shared.kUserEmail, def: '');
      imageUrl = Shared.getString(Shared.kImageUrl, def: '');
    });
  }

  ///Use this method to save user data to shared preferences and fire store
  @override
  Future<void> save() {
    saveToShared();
    return super.save();
  }

  ///Use this method to save user data to shared preferences
  void saveToShared() {
    Shared.setString(Shared.kUserId, id);
    Shared.setString(Shared.kUserName, name);
    Shared.setBool(Shared.kIsSignedIn, isSignedIn);
    Shared.setInt(Shared.kAuthType, authType.index);
    Shared.setString(Shared.kUserEmail, email);
    Shared.setString(Shared.kImageUrl, imageUrl);
  }


  @override
  Map<String, dynamic> toJson() {
    return _$UserToJson(this);
  }

  @override
  User formJson(Map<String, dynamic> json) {
    return _$UserFromJson(json);
  }
}

///Available authentication types
enum AuthType {
  google, kakao, email, none
}

```///Extend this class for models to store in firestore db
///
///use [BaseListModel] to store model as list
abstract class BaseModel implements Serializable {
///Base collection of model document
///
///By default base [baseCollection] is user firebase uid,
///you can change it by passing [baseCollection] as constructor argument
@JsonKey(ignore: true)
CollectionReference baseCollection;

///Document to store model inside that will name with [modelName]
@JsonKey(ignore: true)
DocumentReference document;

///Name of this class that will save as document in firestore
@JsonKey(ignore: true)
final String modelName;

BaseModel(this.modelName, {CollectionReference baseCollection}) {
initialFromShared();
}

void initialFromShared() {
String userId;
if(!Shared.initialized()) {
Shared.load().then((preferences) {
userId = Shared.getString(Shared.kUserId);
//if baseCollection was't set, get user firebase uid from shared to unique collection name
if(userId != null) {
this.baseCollection =
baseCollection ?? Firestore.instance.collection(userId);
this.document = this.baseCollection.document(modelName);
}
});
} else {
userId = Shared.getString(Shared.kUserId);
//if baseCollection was't set, get user firebase uid from shared to unique collection name
if(userId != null) {
this.baseCollection =
baseCollection ?? Firestore.instance.collection(userId);
this.document = this.baseCollection.document(modelName);
}
}
}

///Use this method to retrieve model data from firestore
Future singleQuery() async {
DocumentSnapshot documentSnapshot = await document.get();
return this.formJson(documentSnapshot.data);
}

///After initializing model use this method to store data
Future save() async {
Map json = this.toJson();
document.setData(json);
}
}


// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'user.dart';

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

User _$UserFromJson(Map json) {
return User()..name = json['name'] as String;
}

Map _$UserToJson(User instance) =>
{'name': instance.name};

```

flutter 1.2.1
dart 2.1.2

Most helpful comment

Try replaying the full URI for the part file with just

part 'user.g.dart';".

All 3 comments

Try replaying the full URI for the part file with just

part 'user.g.dart';".

@kevmoo thanks! it works like charm!
but i cant understand the difference, both of are refer to user.g.dart file.

Glad you fixed it!

Was this page helpful?
0 / 5 - 0 ratings