google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 394 forks source link

Does not generate .g.dart files any more #424

Closed HosseinArabbeigi closed 5 years ago

HosseinArabbeigi commented 5 years ago

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
}
///
///use [BaseListModel] to store model as list
abstract class BaseModel<T> implements Serializable<T> {
  ///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<T> singleQuery() async {
    DocumentSnapshot documentSnapshot = await document.get();
    return this.formJson(documentSnapshot.data);
  }

  ///After initializing model use this method to store data
  Future<void> save() async {
    Map json = this.toJson();
    document.setData(json);
  }
}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'user.dart';

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

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

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

flutter 1.2.1 dart 2.1.2

kevmoo commented 5 years ago

Try replaying the full URI for the part file with just

part 'user.g.dart';".
HosseinArabbeigi commented 5 years ago

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

kevmoo commented 5 years ago

Glad you fixed it!

almasud commented 3 years ago

Not solved the problem for me!!!