Skip to content

I really like get generate model #241

@coderxsleee

Description

@coderxsleee

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions