Answers for "flutter json to model"

5

flutter json to list

List to JSON: 
_______________________________________________________________________________
List<String> fruits = ['Apple', 'Banana', 'Orange', 'Strawberry'];
String jsonFruits = jsonEncode(fruits);

________________________________________________________________________________
JSON to List:
________________________________________________________________________________
You need to convert each item individually

	var json = jsonEncode(jsonFruits.map((e) => e.toJson()).toList());

or pass an toEncodable function

	var json = jsonEncode(jsonFruits, toEncodable: (e) => e.toJsonAttr());
Posted by: Guest on May-20-2021
0

json to list flutter

List<UserModel> users = (json.decode(response.body) as List)
      .map((data) => UserModel.fromJson(data))
      .toList();
Posted by: Guest on July-28-2021
1

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);
Posted by: Guest on March-20-2021
0

dart to json

class Testing {
  String guid;
  int index;
  String favoriteFruit;
  double latitude;
  String company;
  String email;
  String picture;
  List<String> tags;
  String registered;
  String eyeColor;
  String phone;
  String address;
  List<Friends> friends;
  bool isActive;
  String about;
  String balance;
  String name;
  String gender;
  int age;
  String greeting;
  double longitude;
  String sId;

  Testing(
      {this.guid,
      this.index,
      this.favoriteFruit,
      this.latitude,
      this.company,
      this.email,
      this.picture,
      this.tags,
      this.registered,
      this.eyeColor,
      this.phone,
      this.address,
      this.friends,
      this.isActive,
      this.about,
      this.balance,
      this.name,
      this.gender,
      this.age,
      this.greeting,
      this.longitude,
      this.sId});

  Testing.fromJson(Map<String, dynamic> json) {
    guid = json['guid'];
    index = json['index'];
    favoriteFruit = json['favoriteFruit'];
    latitude = json['latitude'];
    company = json['company'];
    email = json['email'];
    picture = json['picture'];
    tags = json['tags'].cast<String>();
    registered = json['registered'];
    eyeColor = json['eyeColor'];
    phone = json['phone'];
    address = json['address'];
    if (json['friends'] != null) {
      friends = new List<Friends>();
      json['friends'].forEach((v) {
        friends.add(new Friends.fromJson(v));
      });
    }
    isActive = json['isActive'];
    about = json['about'];
    balance = json['balance'];
    name = json['name'];
    gender = json['gender'];
    age = json['age'];
    greeting = json['greeting'];
    longitude = json['longitude'];
    sId = json['_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['guid'] = this.guid;
    data['index'] = this.index;
    data['favoriteFruit'] = this.favoriteFruit;
    data['latitude'] = this.latitude;
    data['company'] = this.company;
    data['email'] = this.email;
    data['picture'] = this.picture;
    data['tags'] = this.tags;
    data['registered'] = this.registered;
    data['eyeColor'] = this.eyeColor;
    data['phone'] = this.phone;
    data['address'] = this.address;
    if (this.friends != null) {
      data['friends'] = this.friends.map((v) => v.toJson()).toList();
    }
    data['isActive'] = this.isActive;
    data['about'] = this.about;
    data['balance'] = this.balance;
    data['name'] = this.name;
    data['gender'] = this.gender;
    data['age'] = this.age;
    data['greeting'] = this.greeting;
    data['longitude'] = this.longitude;
    data['_id'] = this.sId;
    return data;
  }
}

class Friends {
  int id;
  String name;

  Friends({this.id, this.name});

  Friends.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  }
}
Posted by: Guest on July-23-2021

Code answers related to "Dart"

Browse Popular Code Answers by Language