jonataslaw / get_cli

Official Getx CLI
Apache License 2.0
578 stars 165 forks source link

I really like get generate model #241

Open coderleexs opened 6 months ago

coderleexs commented 6 months ago

The JSON returned by the server is sometimes non-standard, such as:

user.json

[{
  "name": "",
  "age": 0,
  "discount": 0,
  "friends": []
},{
  "name": "",
  "age": 0,
  "discount": 0.0,
  "friends": []
}]

Run:get generate model on home with assets/models/user.json

output:

class User {
  String? name;
  int? age;
  double? discount;
  List<Null>? friends;

  User({this.name, this.age, this.discount, this.friends});

  User.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    age = json['age'];
    discount = json['discount'];
    if (json['friends'] != null) {
      friends = <Null>[];
      json['friends'].forEach((v) {
        friends?.add(Null.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    data['name'] = name;
    data['age'] = age;
    data['discount'] = discount;
    if (friends != null) {
      data['friends'] = friends?.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

flutter run: [Error: fluent/runtime/start_vm_initializer. cc (41)] Unhandled Exception: type 'int' is not a subtype of type 'double?'

I will double? Change to num? After solving this problem, how do I mention int when generating the model Double? Unified replacement with num? Type?

knottx commented 5 months ago

I've added this fixed wait for release https://github.com/jonataslaw/get_cli/pull/221