Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated source.md file to include JSON to Dart Model converter #626

Closed
wants to merge 1 commit into from

Conversation

VPetar
Copy link

@VPetar VPetar commented Mar 3, 2020

Json to Dart website/app is a neat lil' helper that I wish I had since the day 1 when I started delving into the Flutter world.

The way I use it is like this:

  • Open up my Postman API Client
  • Make a request to an API endpoint and receive some JSON as a response
  • Copy "raw" JSON that you just got
  • Open up the Json to Dart app
  • Paste the JSON over there, select class name and click "generate" blue button

VOILA! I get a FULLY mapped model that will accompany my HTTP responses. Literally, that's all there is to it.

I use http Dart package to make (async) HTTP requests, get the response body, use ('dart:convert') json.decode as a parameter in the constructor class_name.fromJson(json.decode(response.body)) and BOOM - there it is. :)

JSON is a bit weird in my opinion in the flutter/dart world this baby makes everything much easier, with automated model generation. :)

Also, I'm including here a full json/converted model example:

JSON

{
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "color": "gold",
  "number": 123,
  "object": {
    "a": "b",
    "c": "d"
  },
  "string": "Hello World",
  "also_a_complicated_set_of_something": [
    {
      "type": "one",
      "qwe": "qweqwe"
    },
    {
      "type": "one two",
      "qwe": "qweqwe"
    },
    {
      "type": "one two three",
      "qwe": "qweqwe"
    },
    {
      "type": "one two three four",
      "qwe": "qweqwe"
    }
  ]
}

Dart model

class SomeModelNameHere {
  List<int> array;
  bool boolean;
  String color;
  int number;
  Object object;
  String string;
  List<AlsoAComplicatedSetOfSomething> alsoAComplicatedSetOfSomething;

  SomeModelNameHere(
      {this.array,
      this.boolean,
      this.color,
      this.number,
      this.object,
      this.string,
      this.alsoAComplicatedSetOfSomething});

  SomeModelNameHere.fromJson(Map<String, dynamic> json) {
    array = json['array'].cast<int>();
    boolean = json['boolean'];
    color = json['color'];
    number = json['number'];
    object =
        json['object'] != null ? new Object.fromJson(json['object']) : null;
    string = json['string'];
    if (json['also_a_complicated_set_of_something'] != null) {
      alsoAComplicatedSetOfSomething =
          new List<AlsoAComplicatedSetOfSomething>();
      json['also_a_complicated_set_of_something'].forEach((v) {
        alsoAComplicatedSetOfSomething
            .add(new AlsoAComplicatedSetOfSomething.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['array'] = this.array;
    data['boolean'] = this.boolean;
    data['color'] = this.color;
    data['number'] = this.number;
    if (this.object != null) {
      data['object'] = this.object.toJson();
    }
    data['string'] = this.string;
    if (this.alsoAComplicatedSetOfSomething != null) {
      data['also_a_complicated_set_of_something'] =
          this.alsoAComplicatedSetOfSomething.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Object {
  String a;
  String c;

  Object({this.a, this.c});

  Object.fromJson(Map<String, dynamic> json) {
    a = json['a'];
    c = json['c'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['a'] = this.a;
    data['c'] = this.c;
    return data;
  }
}

class AlsoAComplicatedSetOfSomething {
  String type;
  String qwe;

  AlsoAComplicatedSetOfSomething({this.type, this.qwe});

  AlsoAComplicatedSetOfSomething.fromJson(Map<String, dynamic> json) {
    type = json['type'];
    qwe = json['qwe'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['type'] = this.type;
    data['qwe'] = this.qwe;
    return data;
  }
}

Thanks for reading my wall of text, hope this can help. :)

DISCLAIMER:
I am NOT the author of this app, I just found it randomly in the foggy mists of the internet, but I believe this deserves to get it's place in this awesome awesome-flutter collection.

Many thanks!

@Solido
Copy link
Owner

Solido commented Mar 4, 2020

This belong to awesome dart !
https://github.com/yissachar/awesome-dart

@VPetar
Copy link
Author

VPetar commented Mar 5, 2020

Hm... I agree. Thank you, I'll do a pull request over there.

@VPetar VPetar closed this Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants