Skip to content

Instantly share code, notes, and snippets.

@achinverma
Created July 4, 2021 05:43
Show Gist options
  • Save achinverma/014d181caf41859a62200d6f5444fc65 to your computer and use it in GitHub Desktop.
Save achinverma/014d181caf41859a62200d6f5444fc65 to your computer and use it in GitHub Desktop.
// To parse this JSON data, do
//
// final postResponse = postResponseFromJson(jsonString);
import 'dart:convert';
PostResponse postResponseFromJson(String str) => PostResponse.fromJson(json.decode(str));
String postResponseToJson(PostResponse data) => json.encode(data.toJson());
class PostResponse {
PostResponse({
this.code,
this.data,
});
int code;
List<Datum> data;
factory PostResponse.fromJson(Map<String, dynamic> json) => PostResponse(
code: json["code"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"code": code,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
this.id,
this.userId,
this.title,
this.body,
this.createdAt,
this.updatedAt,
});
int id;
int userId;
String title;
String body;
DateTime createdAt;
DateTime updatedAt;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
id: json["id"],
userId: json["user_id"],
title: json["title"],
body: json["body"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"user_id": userId,
"title": title,
"body": body,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment