flutter json to object code example

Example 1: flutter generate json files

flutter packages pub run build_runner build

Example 2: parse json to dart model

import 'dart:convert';

main() {
  String nestedObjText =
      '{"title": "Dart Tutorial", "description": "Way to parse Json", "author": {"name": "bezkoder", "age": 30}}';

  Tutorial tutorial = Tutorial.fromJson(jsonDecode(nestedObjText));

  print(tutorial);

Example 3: flutter json to class

//insideclass
ClassName.fromJson(Map<String, dynamic> json) {
    variable1 = json['variable11'];
    variable2 = json['variable12'];
    variable3 = json['variable13']['variable14'];
}

//uses
//after http request or whatever
Map<dynamic, dynamic> res = await jsonDecode(response.body.toString());
Classname.fromJson(res);