How to parse a graphql response in flutter - json

I am trying to parse my graphql response from my neo4j-graphql backend in my flutter app. I am using the flutter_graphql plugin to make queries to the back end. However, when I try to parse the response(JSON) I am getting 'LinkHashMap is not a subtype of Users Map.'
I tried modifying my serializing classes that will parse the response but to no available .below is the JSON response from neo4j graphql
/*I HAVE EDITED THE JSON RESPONSE. THE FULL JSON RESPONSE FROM THE SERVER IS AS BELOW*/
{
"data": {
"User": [
{
"userId": 1,
"city": "NewYork",
"country": "User",
"captionType": "User",
"state": "New York",
"firstName": "example2",
"lastname": "example",
"email": "example2#gmail.com"
}
]
},
"extensions": {
"type": "READ_ONLY"
}
}
below are the classes that represent the above response
#JsonSerializable()
class User {
final int userId;
final String email ;
final String country ;
final String firstName;
final String gender ;
final String city ;
final String dateOfBirth ;
final String state;
final String captionType;
final String lastname;
User({this.userId,this.email,this.country,this.firstName,this.gender,this.dateOfBirth,this.state,this.captionType,this.city,this.lastname});
factory User.fromJson(Map<String,dynamic> json)=> _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
class Users{
final List<User>users;
Users({this.users});
factory Users.fromJson(Map<String, dynamic> parsedJson){
var list = parsedJson['users'] as List;
List<User> usersList = list.map((i) => User.fromJson(i)).toList();
return Users(
users: usersList
);
}
}
//below is the graphql configuration in my seperate GraphQl.dart file
class GraphQLConfiguration {
static HttpLink httpLink = HttpLink(
uri: "http://localhost:7474/graphql/",
headers: {
HttpHeaders.authorizationHeader: ************",
},
);
static final AuthLink authLink = AuthLink(
getToken: () async => 'Bearer <YOUR_PERSONAL_ACCESS_TOKEN>',
// OR
// getToken: () => 'Bearer <YOUR_PERSONAL_ACCESS_TOKEN>',
);
static ValueNotifier<GraphQLClient> initailizeClient() {
ValueNotifier<GraphQLClient> client = ValueNotifier(
GraphQLClient(
cache: InMemoryCache(),
link: httpLink,
),
);
return client;
}
static GraphQLClient clientToQuery() {
return GraphQLClient(
cache: OptimisticCache(
dataIdFromObject: typenameDataIdFromObject),
link: httpLink,
);
}
}
//below is my main.dart file where am trying to parse the response
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: true,
title: 'Flutter Graphql Client',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/': (context) => MyHomePage(), //RootPage(auth: new Auth(),),
},
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget listViewWidget(List<Users> users) {
return MediaQuery.removePadding(
context: context, removeTop: true,
child: ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
return Container(
child: Column(
children: <Widget>[
Text('users:$users'),
],
),
);
}),
);
}
String readUser = """
query{
User(userId:1){
userId
city
country
captionType
state
firstName
lastname
email
}
}
""";
#override
Widget build(BuildContext context) {
return GraphQLProvider(
client: GraphQLConfiguration.initailizeClient(),
child: CacheProvider(
child: Scaffold(
appBar: AppBar(
title: Text('Flutter Graphql Client'),
),
body:Query(
options: QueryOptions(
document: readUser),
builder: (QueryResult result, {VoidCallback refetch, FetchMore fetchMore}) {
if (result.errors!= null) {
print('$result.errors');
return Text(result.errors.toString());
} if (result.errors== null){
print(('$result'));
print(('${result.data}'));
} if (result.loading){
return Center(
child: CupertinoActivityIndicator(
radius: 13.0,
));
}
return listViewWidget(result.data);
},
)
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
),
));
}
}
I am expecting the info to be parsed through the Users class and displayed through the listViewWidget. However I am getting'LinkHashMap is not a subtype of Users Map.'

You can parse any JSON using https://app.quicktype.io/, below is the model class for your JSON
// To parse this JSON data, do
//
// final responseModel = responseModelFromJson(jsonString);
import 'dart:convert';
ResponseModel responseModelFromJson(String str) => ResponseModel.fromJson(json.decode(str));
String responseModelToJson(ResponseModel data) => json.encode(data.toJson());
class ResponseModel {
Data data;
Extensions extensions;
ResponseModel({
this.data,
this.extensions,
});
factory ResponseModel.fromJson(Map<String, dynamic> json) => ResponseModel(
data: json["data"] == null ? null : Data.fromJson(json["data"]),
extensions: json["extensions"] == null ? null : Extensions.fromJson(json["extensions"]),
);
Map<String, dynamic> toJson() => {
"data": data == null ? null : data.toJson(),
"extensions": extensions == null ? null : extensions.toJson(),
};
}
class Data {
List<User> user;
Data({
this.user,
});
factory Data.fromJson(Map<String, dynamic> json) => Data(
user: json["User"] == null ? null : List<User>.from(json["User"].map((x) => User.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"User": user == null ? null : List<dynamic>.from(user.map((x) => x.toJson())),
};
}
class User {
int userId;
String city;
String country;
String captionType;
String state;
String firstName;
String lastname;
String email;
User({
this.userId,
this.city,
this.country,
this.captionType,
this.state,
this.firstName,
this.lastname,
this.email,
});
factory User.fromJson(Map<String, dynamic> json) => User(
userId: json["userId"] == null ? null : json["userId"],
city: json["city"] == null ? null : json["city"],
country: json["country"] == null ? null : json["country"],
captionType: json["captionType"] == null ? null : json["captionType"],
state: json["state"] == null ? null : json["state"],
firstName: json["firstName"] == null ? null : json["firstName"],
lastname: json["lastname"] == null ? null : json["lastname"],
email: json["email"] == null ? null : json["email"],
);
Map<String, dynamic> toJson() => {
"userId": userId == null ? null : userId,
"city": city == null ? null : city,
"country": country == null ? null : country,
"captionType": captionType == null ? null : captionType,
"state": state == null ? null : state,
"firstName": firstName == null ? null : firstName,
"lastname": lastname == null ? null : lastname,
"email": email == null ? null : email,
};
}
class Extensions {
String type;
Extensions({
this.type,
});
factory Extensions.fromJson(Map<String, dynamic> json) => Extensions(
type: json["type"] == null ? null : json["type"],
);
Map<String, dynamic> toJson() => {
"type": type == null ? null : type,
};
}
use this code to parse your response
ResponseModel responseModel = responseModelFromJson(result.data);
return listViewWidget(responseModel.data.user);

Related

Stuck with json in flutter

I dont know the problem is because when debuging there isn't error info but the value of json is empty when I print info.lenght
class _HomePageState extends State<HomePage> {
List info = [];
_initData(){
DefaultAssetBundle.of(context).loadString("json/info.json").then((value){
info = jsonDecode(value);
});
}
#override
void iniState(){
super.initState();
_initData();
}
and below is my json code in json/info.json
[
{
"title": "Glue",
"img": "assets/ex1.png"
},
{
"title": "Abs",
"img": "assets/ex2.png"
},
{
"title": "Legs",
"img": "assets/ex3.png"
},
{
"title": "Arms",
"img": "assets/ex4.png"
}
]
and how to we print the img and title value of json in dart?
You have to first create a model class for your json response.
// To parse this JSON data, do
//
// final jsonModel = jsonModelFromJson(jsonString);
import 'dart:convert';
List<JsonModel> jsonModelFromJson(String str) => List<JsonModel>.from(json.decode(str).map((x) => JsonModel.fromJson(x)));
String jsonModelToJson(List<JsonModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class JsonModel {
JsonModel({
this.title,
this.img,
});
String title;
String img;
factory JsonModel.fromJson(Map<String, dynamic> json) => JsonModel(
title: json["title"] == null ? null : json["title"],
img: json["img"] == null ? null : json["img"],
);
Map<String, dynamic> toJson() => {
"title": title == null ? null : title,
"img": img == null ? null : img,
};
}
then just use this model class to decode your json file.
var result = JsonModel.fromJson(jsonResponse);
Later, you can just use
result.title or result.img to get the data
You have to add your json file into pubspec.yaml file.
flutter:
assets:
- json/info.json
Before loading page view, you should load values from your json file and assign a list in initState. After that, you can use list as map by [index][header].
class JsonResult extends StatefulWidget {
const JsonResult({Key? key}) : super(key: key);
#override
_JsonResultState createState() => _JsonResultState();
}
class _JsonResultState extends State<JsonResult> {
var jsonList = [];
#override
void initState() {
rootBundle.loadString("json/info.json").then((value) {
jsonList = json.decode(value);
});
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView.builder(
itemBuilder: (context, index) {
return ListTile(
leading: Image.asset(jsonList[index]["img"]),
title: Text(jsonList[index]["title"]),
);
},
itemCount: jsonList.length,
),
),
);
}
}

How to parse a complex Json value in flutter?

I have a JSON which is a combination of simple and complicated structures.Anyhow you can have a look at it.All I want is to get the "playlist_url": value and display it a listview builder.Along with that I want to parse for 2 text values which I am able to do.But the url part is where I am not able to resolve
The JSON structure(it is too long,thats y I have given the link): https://docs.google.com/document/d/1saJN3MQvG55M1ipf42-65Etowi_kW80gkrosU6vBb5o/edit?usp=sharing
The PODO file:
// To parse this JSON data, do
//
// final homePage = homePageFromJson(jsonString);
import 'dart:convert';
HomePage homePageFromJson(String str) => HomePage.fromJson(json.decode(str));
String homePageToJson(HomePage data) => json.encode(data.toJson());
class HomePage {
HomePage({
this.series,
this.homeBanners,
this.liveChannels,
this.publishers,
this.musicCategories,
this.musicPlaylists,
this.movies,
});
List<HomeBanner> series;
List<HomeBanner> homeBanners;
List<LiveChannel> liveChannels;
List<Publisher> publishers;
List<Music> musicCategories;
Music musicPlaylists;
List<HomeBanner> movies;
factory HomePage.fromJson(Map<String, dynamic> json) => HomePage(
series: List<HomeBanner>.from(
json["series"].map((x) => HomeBanner.fromJson(x))),
homeBanners: List<HomeBanner>.from(
json["home_banners"].map((x) => HomeBanner.fromJson(x))),
liveChannels: List<LiveChannel>.from(
json["live_channels"].map((x) => LiveChannel.fromJson(x))),
publishers: List<Publisher>.from(
json["publishers"].map((x) => Publisher.fromJson(x))),
musicCategories: List<Music>.from(
json["music_categories"].map((x) => Music.fromJson(x))),
musicPlaylists: Music.fromJson(json["music_playlists"]),
movies: List<HomeBanner>.from(
json["movies"].map((x) => HomeBanner.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"series": List<dynamic>.from(series.map((x) => x.toJson())),
"home_banners": List<dynamic>.from(homeBanners.map((x) => x.toJson())),
"live_channels":
List<dynamic>.from(liveChannels.map((x) => x.toJson())),
"publishers": List<dynamic>.from(publishers.map((x) => x.toJson())),
"music_categories":
List<dynamic>.from(musicCategories.map((x) => x.toJson())),
"music_playlists": musicPlaylists.toJson(),
"movies": List<dynamic>.from(movies.map((x) => x.toJson())),
};
}
class HomeBanner {
HomeBanner({
this.movieId,
this.title,
this.tags,
this.genres,
this.thumbnail,
this.posterLink,
this.platform,
this.worldwide,
this.createdAt,
this.seriesId,
});
String movieId;
String title;
List<String> tags;
List<String> genres;
List<String> thumbnail;
String posterLink;
Platform platform;
double worldwide;
DateTime createdAt;
String seriesId;
factory HomeBanner.fromJson(Map<String, dynamic> json) => HomeBanner(
movieId: json["movie_id"] == null ? null : json["movie_id"],
title: json["title"],
tags: List<String>.from(json["tags"].map((x) => x)),
genres: List<String>.from(json["genres"].map((x) => x)),
thumbnail: List<String>.from(json["thumbnail"].map((x) => x)),
posterLink: json["poster_link"],
platform: platformValues.map[json["platform"]],
worldwide: json["WORLDWIDE"],
createdAt: DateTime.parse(json["createdAt"]),
seriesId: json["series_id"] == null ? null : json["series_id"],
);
Map<String, dynamic> toJson() => {
"movie_id": movieId == null ? null : movieId,
"title": title,
"tags": List<dynamic>.from(tags.map((x) => x)),
"genres": List<dynamic>.from(genres.map((x) => x)),
"thumbnail": List<dynamic>.from(thumbnail.map((x) => x)),
"poster_link": posterLink,
"platform": platformValues.reverse[platform],
"WORLDWIDE": worldwide,
"createdAt": createdAt.toIso8601String(),
"series_id": seriesId == null ? null : seriesId,
};
}
enum Platform { YOUTUBE, DISCOVERYPLUS }
final platformValues = EnumValues(
{"discoveryplus": Platform.DISCOVERYPLUS, "youtube": Platform.YOUTUBE});
class LiveChannel {
LiveChannel({
this.keyId,
this.postContent,
this.publisherId,
this.publisherName,
this.publisherProfilePic,
this.publisherDesc,
this.downvotesCount,
this.upvotesCount,
});
String keyId;
PostContent postContent;
String publisherId;
String publisherName;
String publisherProfilePic;
String publisherDesc;
int downvotesCount;
int upvotesCount;
factory LiveChannel.fromJson(Map<String, dynamic> json) => LiveChannel(
keyId: json["key_id"],
postContent: PostContent.fromJson(json["post_content"]),
publisherId: json["publisher_id"],
publisherName: json["publisher_name"],
publisherProfilePic: json["publisher_profile_pic"],
publisherDesc: json["publisher_desc"],
downvotesCount: json["downvotes_count"],
upvotesCount: json["upvotes_count"],
);
Map<String, dynamic> toJson() => {
"key_id": keyId,
"post_content": postContent.toJson(),
"publisher_id": publisherId,
"publisher_name": publisherName,
"publisher_profile_pic": publisherProfilePic,
"publisher_desc": publisherDesc,
"downvotes_count": downvotesCount,
"upvotes_count": upvotesCount,
};
}
class PostContent {
PostContent({
this.shortcode,
this.platformVideoLink,
this.caption,
this.description,
});
String shortcode;
String platformVideoLink;
String caption;
String description;
factory PostContent.fromJson(Map<String, dynamic> json) => PostContent(
shortcode: json["shortcode"],
platformVideoLink: json["platform_videoLink"],
caption: json["caption"],
description: json["description"],
);
Map<String, dynamic> toJson() => {
"shortcode": shortcode,
"platform_videoLink": platformVideoLink,
"caption": caption,
"description": description,
};
}
class Music {
Music({
this.id,
this.country,
this.categoryId,
this.categoryName,
this.categoryIcons,
this.playlists,
});
dynamic id;
String country;
String categoryId;
String categoryName;
List<CategoryIcon> categoryIcons;
List<Playlist> playlists;
factory Music.fromJson(Map<String, dynamic> json) => Music(
id: json["_id"],
country: json["country"],
categoryId: json["category_id"],
categoryName: json["category_name"],
categoryIcons: List<CategoryIcon>.from(
json["category_icons"].map((x) => CategoryIcon.fromJson(x))),
playlists: List<Playlist>.from(
json["playlists"].map((x) => Playlist.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"_id": id,
"country": country,
"category_id": categoryId,
"category_name": categoryName,
"category_icons":
List<dynamic>.from(categoryIcons.map((x) => x.toJson())),
"playlists": List<dynamic>.from(playlists.map((x) => x.toJson())),
};
}
class CategoryIcon {
CategoryIcon({
this.height,
this.url,
this.width,
});
int height;
String url;
int width;
factory CategoryIcon.fromJson(Map<String, dynamic> json) => CategoryIcon(
height: json["height"] == null ? null : json["height"],
url: json["url"],
width: json["width"] == null ? null : json["width"],
);
Map<String, dynamic> toJson() => {
"height": height == null ? null : height,
"url": url,
"width": width == null ? null : width,
};
}
class Playlist {
Playlist({
this.playlistName,
this.playlistDescription,
this.playlistUrl,
this.playlistTotalTracks,
this.playlistImages,
this.playlistFollowers,
this.playlistId,
});
String playlistName;
String playlistDescription;
String playlistUrl;
int playlistTotalTracks;
List<CategoryIcon> playlistImages;
int playlistFollowers;
String playlistId;
factory Playlist.fromJson(Map<String, dynamic> json) => Playlist(
playlistName: json["playlist_name"],
playlistDescription: json["playlist_description"],
playlistUrl: json["playlist_url"],
playlistTotalTracks: json["playlist_total_tracks"],
playlistImages: List<CategoryIcon>.from(
json["playlist_images"].map((x) => CategoryIcon.fromJson(x))),
playlistFollowers: json["playlist_followers"],
playlistId: json["playlist_id"],
);
Map<String, dynamic> toJson() => {
"playlist_name": playlistName,
"playlist_description": playlistDescription,
"playlist_url": playlistUrl,
"playlist_total_tracks": playlistTotalTracks,
"playlist_images":
List<dynamic>.from(playlistImages.map((x) => x.toJson())),
"playlist_followers": playlistFollowers,
"playlist_id": playlistId,
};
}
class Publisher {
Publisher({
this.platform,
this.username,
this.fullName,
this.profilePicUrl,
this.content,
this.keyId,
});
Platform platform;
String username;
String fullName;
String profilePicUrl;
Content content;
String keyId;
factory Publisher.fromJson(Map<String, dynamic> json) => Publisher(
platform: platformValues.map[json["platform"]],
username: json["username"],
fullName: json["full_name"],
profilePicUrl: json["profile_pic_url"],
content: Content.fromJson(json["content"]),
keyId: json["key_id"],
);
Map<String, dynamic> toJson() => {
"platform": platformValues.reverse[platform],
"username": username,
"full_name": fullName,
"profile_pic_url": profilePicUrl,
"content": content.toJson(),
"key_id": keyId,
};
}
class Content {
Content({
this.description,
});
String description;
factory Content.fromJson(Map<String, dynamic> json) => Content(
description: json["description"],
);
Map<String, dynamic> toJson() => {
"description": description,
};
}
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
The class called services where I am trying to parse the playlist_url.I am able to parse the playlist name and the number of videos of it(the count of it)
class Services {
static const String url =
"https://livetvapi.apyhi.com/api/v2/home?pageLocation=home&countries=IN&app_version=13&"
"user_id=44edc2c905ae163f&package_id=livetv.movies.freemovies.watchtv.tvshows&os_platform=android";
static Future<List<String>> loadDataForPlaylistDetailsForButtomTitle() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is there");
final homePage = homePageFromJson(res.body);
Playlist playListObject = new Playlist();
List<String> lst_names = [];
for (playListObject in homePage.musicPlaylists.playlists)
lst_names.add(playListObject.playlistName);
print("Buttom titles");
print(lst_names);
return lst_names;
} else {
print("no response");
return null;
}
}
static Future<List<String>> loadDataForPlaylistDetailsForCount() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is there");
final homePage = homePageFromJson(res.body);
Playlist playListObject = new Playlist();
List<String> lst_names = [];
for (playListObject in homePage.musicPlaylists.playlists)
lst_names.add(playListObject.playlistTotalTracks.toString());
print("count");
print(lst_names);
return lst_names;
} else {
print("no response");
return null;
}
}
static Future<List<Playlist>> loadDataForPlaylistDetailsForImageUrl() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is there");
final homePage = homePageFromJson(res.body);
Music musicObject = new Music();
List<Playlist> playlistObj = homePage.musicPlaylists.playlists;
print("category icon object returned");
return playlistObj;
} else {
print("no response");
return null;
}
}
}
This is the main file where I am trying to display in listView.First the data is loaded in initstate and then stored in arrays.But for the last one (the url stuff)I tried with the object it self as things became complicated
#override
void initState() {
// TODO: implement initState
super.initState();
Services.loadDataForPlaylistDetailsForButtomTitle().then((playListNames) {
setState(() {
_playListNames = playListNames ;
});
});
Services.loadDataForPlaylistDetailsForButtomTitle().then((playListCount) {
setState(() {
_playListtotalTracks = playListCount ;
});
});
Services.loadDataForPlaylistDetailsForImageUrl().then((objUrl) {
setState(() {
_obj = objUrl ;
});
});
}
The code for listView Builder:
Container(
height: MediaQuery.of(context).size.height * 0.41,
color: Colors.black,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: _playListImageUrls.length,
itemBuilder: (BuildContext context, int index) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
height: MediaQuery.of(context).size.height * 0.34,
child: PhysicalModel(
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Colors.black,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.only(
topRight: Radius.circular(35),
bottomRight: Radius.circular(35)),
child: FadeInImage.assetNetwork(
width: MediaQuery.of(context).size.width * 0.285,
image: _obj[index].playlistImages[index].url,
placeholder: cupertinoActivityIndicator,
fit: BoxFit.fill,
),
),
),
Container(
margin: EdgeInsets.fromLTRB(15, 15, 0, 0),
height: MediaQuery.of(context).size.height * 0.03,
child: Text(
_playListNames[index],
style: TextStyle(color: Colors.white),
)),
Container(
margin: EdgeInsets.fromLTRB(15, 0, 0, 0),
height: MediaQuery.of(context).size.height * 0.02,
child: Text(
_playListtotalTracks[index],
style: TextStyle(color: Colors.white),
))
],
),
),
),
Also I find myself repeating this structures many times.Any suggestions to improvise would be equally welcomed.
I have finally found the solution.The whole point where I was doing the mistake was assuming things as a list of object,rather it was a list of list of single object.
The following modifications were made:
In service file
static Future<List<String>> loadDataForPlaylistDetailsForImageUrl() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is thereeeeeeeee");
final homePage = homePageFromJson(res.body);
Playlist playListObject = new Playlist();
List<String> urlList = [];
List<dynamic> lst_names = [];
for (playListObject in homePage.musicPlaylists.playlists) {
lst_names.add(playListObject.playlistImages);
print(lst_names);
}
//lst_names=
print("objjjjjjjjjjjjjjjjjjjj");
for (var listobj in lst_names) {
for (var obj in listobj) {
print(obj.url.toString());
urlList.add(obj.url.toString());
}
}
return urlList;
} else {
print("no response");
return null;
}
}
Also in main file:
FadeInImage.assetNetwork(
width: MediaQuery.of(context).size.width * 0.285,
image: _musicPlaylistImgUrlList[index],
//_categoryIconfor[index].url,
//_obj[index].playlistImages[index].url,
placeholder: cupertinoActivityIndicator,
fit: BoxFit.none,
),

Flutter fetch data from the internet

I'm trying to get some information from here such as name,avatar_url,stargazers_count and description
{
"total_count": 18689015,
"incomplete_results": true,
"items": [
{
"id": 215415332,
"node_id": "MDEwOlJlcG9zaXRvcnkyMTU0MTUzMzI=",
"name": "HackingNeuralNetworks",
"full_name": "Kayzaks/HackingNeuralNetworks",
"private": false,
"owner": {
"login": "Kayzaks",
"id": 11071537,
"node_id": "MDQ6VXNlcjExMDcxNTM3",
"avatar_url": "https://avatars1.githubusercontent.com/u/11071537?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Kayzaks",
"html_url": "https://github.com/Kayzaks",
"followers_url": "https://api.github.com/users/Kayzaks/followers",
"following_url": "https://api.github.com/users/Kayzaks/following{/other_user}",
"gists_url": "https://api.github.com/users/Kayzaks/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Kayzaks/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Kayzaks/subscriptions",
"organizations_url": "https://api.github.com/users/Kayzaks/orgs",
"repos_url": "https://api.github.com/users/Kayzaks/repos",
"events_url": "https://api.github.com/users/Kayzaks/events{/privacy}",
"received_events_url": "https://api.github.com/users/Kayzaks/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/Kayzaks/HackingNeuralNetworks",
"description": "A small course on exploiting and defending neural networks",
"fork": false,
....
....
At run time I get this message error :
type _internalLine HashMap<String , dynamic> is not a subtype of type List<dynamic> in type cast
here's the full code :
RepoItem:
class RepoItem {
Owner owner;
String name;
String stargazers_count;
String description;
RepoItem._({this.owner, this.name, this.stargazers_count, this.description});
factory RepoItem.fromJson(Map<String, dynamic> json) {
return new RepoItem._(
owner: json['owner'],
name: json['name'],
stargazers_count: json['stargazers_count'],
description: json['description']);
}
}
PageState:
class _MyHomePageState extends State<MyHomePage> {
List<RepoItem> list = List();
var isLoading = false;
Future<List<RepoItem>> _fetchData() async {
final response = await http.get(
"https://api.github.com/search/repositories?q=created:%3E2018-10-22&sort=stars&order=desc");
list = (json.decode(response.body) as List)
.map((data) => new RepoItem.fromJson(data.body))
.toList();
return list;
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
child: FutureBuilder(
future: _fetchData(),
builder: (BuildContext context, AsyncSnapshot asyncSnapshot) {
if (asyncSnapshot.hasError) {
return Container(
child: Center(
child: Text(asyncSnapshot.error.toString()),
),
);
}
if (asyncSnapshot.data == null) {
return Container(
child: Center(
child: Text("Loading ..."),
),
);
} else {
return ListView.builder(
itemCount: asyncSnapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(asyncSnapshot.data[index].name),
leading: CircleAvatar(
backgroundImage: NetworkImage(
asyncSnapshot.data[index].owner.avatar_url),
),
subtitle: Text(asyncSnapshot.data[index].description),
);
},
);
}
},
),
),
);
}
}
You can copy paste run full code below
You can parse with payloadFromJson, you can see Payload class in full code
Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));
...
var items = snapshot.data.items;
return ListView.builder(
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(items[index].name),
leading: CircleAvatar(
backgroundImage:
NetworkImage(items[index].owner.avatarUrl),
),
subtitle: Text(items[index].description),
);
},
);
working demo
full code
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
// To parse this JSON data, do
//
// final payload = payloadFromJson(jsonString);
import 'dart:convert';
Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));
String payloadToJson(Payload data) => json.encode(data.toJson());
class Payload {
String totalCount;
bool incompleteResults;
List<Item> items;
Payload({
this.totalCount,
this.incompleteResults,
this.items,
});
factory Payload.fromJson(Map<String, dynamic> json) => Payload(
totalCount: json["total_count"].toString(),
incompleteResults: json["incomplete_results"],
items: List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"total_count": totalCount,
"incomplete_results": incompleteResults,
"items": List<dynamic>.from(items.map((x) => x.toJson())),
};
}
class Item {
String id;
String nodeId;
String name;
String fullName;
bool private;
Owner owner;
String htmlUrl;
String description;
bool fork;
String url;
String forksUrl;
String keysUrl;
String collaboratorsUrl;
String teamsUrl;
String hooksUrl;
String issueEventsUrl;
String eventsUrl;
String assigneesUrl;
String branchesUrl;
String tagsUrl;
String blobsUrl;
String gitTagsUrl;
String gitRefsUrl;
String treesUrl;
String statusesUrl;
String languagesUrl;
String stargazersUrl;
String contributorsUrl;
String subscribersUrl;
String subscriptionUrl;
String commitsUrl;
String gitCommitsUrl;
String commentsUrl;
String issueCommentUrl;
String contentsUrl;
String compareUrl;
String mergesUrl;
String archiveUrl;
String downloadsUrl;
String issuesUrl;
String pullsUrl;
String milestonesUrl;
String notificationsUrl;
String labelsUrl;
String releasesUrl;
String deploymentsUrl;
DateTime createdAt;
DateTime updatedAt;
DateTime pushedAt;
String gitUrl;
String sshUrl;
String cloneUrl;
String svnUrl;
String homepage;
int size;
int stargazersCount;
int watchersCount;
String language;
bool hasIssues;
bool hasProjects;
bool hasDownloads;
bool hasWiki;
bool hasPages;
int forksCount;
dynamic mirrorUrl;
bool archived;
bool disabled;
int openIssuesCount;
License license;
int forks;
int openIssues;
int watchers;
DefaultBranch defaultBranch;
double score;
Item({
this.id,
this.nodeId,
this.name,
this.fullName,
this.private,
this.owner,
this.htmlUrl,
this.description,
this.fork,
this.url,
this.forksUrl,
this.keysUrl,
this.collaboratorsUrl,
this.teamsUrl,
this.hooksUrl,
this.issueEventsUrl,
this.eventsUrl,
this.assigneesUrl,
this.branchesUrl,
this.tagsUrl,
this.blobsUrl,
this.gitTagsUrl,
this.gitRefsUrl,
this.treesUrl,
this.statusesUrl,
this.languagesUrl,
this.stargazersUrl,
this.contributorsUrl,
this.subscribersUrl,
this.subscriptionUrl,
this.commitsUrl,
this.gitCommitsUrl,
this.commentsUrl,
this.issueCommentUrl,
this.contentsUrl,
this.compareUrl,
this.mergesUrl,
this.archiveUrl,
this.downloadsUrl,
this.issuesUrl,
this.pullsUrl,
this.milestonesUrl,
this.notificationsUrl,
this.labelsUrl,
this.releasesUrl,
this.deploymentsUrl,
this.createdAt,
this.updatedAt,
this.pushedAt,
this.gitUrl,
this.sshUrl,
this.cloneUrl,
this.svnUrl,
this.homepage,
this.size,
this.stargazersCount,
this.watchersCount,
this.language,
this.hasIssues,
this.hasProjects,
this.hasDownloads,
this.hasWiki,
this.hasPages,
this.forksCount,
this.mirrorUrl,
this.archived,
this.disabled,
this.openIssuesCount,
this.license,
this.forks,
this.openIssues,
this.watchers,
this.defaultBranch,
this.score,
});
factory Item.fromJson(Map<String, dynamic> json) => Item(
id: json["id"].toString(),
nodeId: json["node_id"],
name: json["name"],
fullName: json["full_name"],
private: json["private"],
owner: Owner.fromJson(json["owner"]),
htmlUrl: json["html_url"],
description: json["description"] == null ? null : json["description"],
fork: json["fork"],
url: json["url"],
forksUrl: json["forks_url"],
keysUrl: json["keys_url"],
collaboratorsUrl: json["collaborators_url"],
teamsUrl: json["teams_url"],
hooksUrl: json["hooks_url"],
issueEventsUrl: json["issue_events_url"],
eventsUrl: json["events_url"],
assigneesUrl: json["assignees_url"],
branchesUrl: json["branches_url"],
tagsUrl: json["tags_url"],
blobsUrl: json["blobs_url"],
gitTagsUrl: json["git_tags_url"],
gitRefsUrl: json["git_refs_url"],
treesUrl: json["trees_url"],
statusesUrl: json["statuses_url"],
languagesUrl: json["languages_url"],
stargazersUrl: json["stargazers_url"],
contributorsUrl: json["contributors_url"],
subscribersUrl: json["subscribers_url"],
subscriptionUrl: json["subscription_url"],
commitsUrl: json["commits_url"],
gitCommitsUrl: json["git_commits_url"],
commentsUrl: json["comments_url"],
issueCommentUrl: json["issue_comment_url"],
contentsUrl: json["contents_url"],
compareUrl: json["compare_url"],
mergesUrl: json["merges_url"],
archiveUrl: json["archive_url"],
downloadsUrl: json["downloads_url"],
issuesUrl: json["issues_url"],
pullsUrl: json["pulls_url"],
milestonesUrl: json["milestones_url"],
notificationsUrl: json["notifications_url"],
labelsUrl: json["labels_url"],
releasesUrl: json["releases_url"],
deploymentsUrl: json["deployments_url"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
pushedAt: DateTime.parse(json["pushed_at"]),
gitUrl: json["git_url"],
sshUrl: json["ssh_url"],
cloneUrl: json["clone_url"],
svnUrl: json["svn_url"],
homepage: json["homepage"] == null ? null : json["homepage"],
size: json["size"],
stargazersCount: json["stargazers_count"],
watchersCount: json["watchers_count"],
language: json["language"] == null ? null : json["language"],
hasIssues: json["has_issues"],
hasProjects: json["has_projects"],
hasDownloads: json["has_downloads"],
hasWiki: json["has_wiki"],
hasPages: json["has_pages"],
forksCount: json["forks_count"],
mirrorUrl: json["mirror_url"],
archived: json["archived"],
disabled: json["disabled"],
openIssuesCount: json["open_issues_count"],
license:
json["license"] == null ? null : License.fromJson(json["license"]),
forks: json["forks"],
openIssues: json["open_issues"],
watchers: json["watchers"],
defaultBranch: defaultBranchValues.map[json["default_branch"]],
score: json["score"],
);
Map<String, dynamic> toJson() => {
"id": id,
"node_id": nodeId,
"name": name,
"full_name": fullName,
"private": private,
"owner": owner.toJson(),
"html_url": htmlUrl,
"description": description == null ? null : description,
"fork": fork,
"url": url,
"forks_url": forksUrl,
"keys_url": keysUrl,
"collaborators_url": collaboratorsUrl,
"teams_url": teamsUrl,
"hooks_url": hooksUrl,
"issue_events_url": issueEventsUrl,
"events_url": eventsUrl,
"assignees_url": assigneesUrl,
"branches_url": branchesUrl,
"tags_url": tagsUrl,
"blobs_url": blobsUrl,
"git_tags_url": gitTagsUrl,
"git_refs_url": gitRefsUrl,
"trees_url": treesUrl,
"statuses_url": statusesUrl,
"languages_url": languagesUrl,
"stargazers_url": stargazersUrl,
"contributors_url": contributorsUrl,
"subscribers_url": subscribersUrl,
"subscription_url": subscriptionUrl,
"commits_url": commitsUrl,
"git_commits_url": gitCommitsUrl,
"comments_url": commentsUrl,
"issue_comment_url": issueCommentUrl,
"contents_url": contentsUrl,
"compare_url": compareUrl,
"merges_url": mergesUrl,
"archive_url": archiveUrl,
"downloads_url": downloadsUrl,
"issues_url": issuesUrl,
"pulls_url": pullsUrl,
"milestones_url": milestonesUrl,
"notifications_url": notificationsUrl,
"labels_url": labelsUrl,
"releases_url": releasesUrl,
"deployments_url": deploymentsUrl,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
"pushed_at": pushedAt.toIso8601String(),
"git_url": gitUrl,
"ssh_url": sshUrl,
"clone_url": cloneUrl,
"svn_url": svnUrl,
"homepage": homepage == null ? null : homepage,
"size": size,
"stargazers_count": stargazersCount,
"watchers_count": watchersCount,
"language": language == null ? null : language,
"has_issues": hasIssues,
"has_projects": hasProjects,
"has_downloads": hasDownloads,
"has_wiki": hasWiki,
"has_pages": hasPages,
"forks_count": forksCount,
"mirror_url": mirrorUrl,
"archived": archived,
"disabled": disabled,
"open_issues_count": openIssuesCount,
"license": license == null ? null : license.toJson(),
"forks": forks,
"open_issues": openIssues,
"watchers": watchers,
"default_branch": defaultBranchValues.reverse[defaultBranch],
"score": score,
};
}
enum DefaultBranch { MASTER }
final defaultBranchValues = EnumValues({"master": DefaultBranch.MASTER});
class License {
String key;
String name;
String spdxId;
String url;
String nodeId;
License({
this.key,
this.name,
this.spdxId,
this.url,
this.nodeId,
});
factory License.fromJson(Map<String, dynamic> json) => License(
key: json["key"],
name: json["name"],
spdxId: json["spdx_id"],
url: json["url"] == null ? null : json["url"],
nodeId: json["node_id"],
);
Map<String, dynamic> toJson() => {
"key": key,
"name": name,
"spdx_id": spdxId,
"url": url == null ? null : url,
"node_id": nodeId,
};
}
class Owner {
String login;
String id;
String nodeId;
String avatarUrl;
String gravatarId;
String url;
String htmlUrl;
String followersUrl;
String followingUrl;
String gistsUrl;
String starredUrl;
String subscriptionsUrl;
String organizationsUrl;
String reposUrl;
String eventsUrl;
String receivedEventsUrl;
Type type;
bool siteAdmin;
Owner({
this.login,
this.id,
this.nodeId,
this.avatarUrl,
this.gravatarId,
this.url,
this.htmlUrl,
this.followersUrl,
this.followingUrl,
this.gistsUrl,
this.starredUrl,
this.subscriptionsUrl,
this.organizationsUrl,
this.reposUrl,
this.eventsUrl,
this.receivedEventsUrl,
this.type,
this.siteAdmin,
});
factory Owner.fromJson(Map<String, dynamic> json) => Owner(
login: json["login"],
id: json["id"].toString(),
nodeId: json["node_id"],
avatarUrl: json["avatar_url"],
gravatarId: json["gravatar_id"],
url: json["url"],
htmlUrl: json["html_url"],
followersUrl: json["followers_url"],
followingUrl: json["following_url"],
gistsUrl: json["gists_url"],
starredUrl: json["starred_url"],
subscriptionsUrl: json["subscriptions_url"],
organizationsUrl: json["organizations_url"],
reposUrl: json["repos_url"],
eventsUrl: json["events_url"],
receivedEventsUrl: json["received_events_url"],
type: typeValues.map[json["type"]],
siteAdmin: json["site_admin"],
);
Map<String, dynamic> toJson() => {
"login": login,
"id": id,
"node_id": nodeId,
"avatar_url": avatarUrl,
"gravatar_id": gravatarId,
"url": url,
"html_url": htmlUrl,
"followers_url": followersUrl,
"following_url": followingUrl,
"gists_url": gistsUrl,
"starred_url": starredUrl,
"subscriptions_url": subscriptionsUrl,
"organizations_url": organizationsUrl,
"repos_url": reposUrl,
"events_url": eventsUrl,
"received_events_url": receivedEventsUrl,
"type": typeValues.reverse[type],
"site_admin": siteAdmin,
};
}
enum Type { USER, ORGANIZATION }
final typeValues =
EnumValues({"Organization": Type.ORGANIZATION, "User": Type.USER});
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static final String URL = "https://corona.lmao.ninja/countries";
Future _future;
Future<Payload> _fetchData() async {
final response = await http.get(
"https://api.github.com/search/repositories?q=created:%3E2018-10-22&sort=stars&order=desc");
var list = payloadFromJson(response.body);
return list;
}
#override
void initState() {
// TODO: implement initState
super.initState();
_future = _fetchData();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder<Payload>(
future: _future,
builder: (BuildContext context, AsyncSnapshot<Payload> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Input a URL to start');
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
case ConnectionState.active:
return Text('');
case ConnectionState.done:
if (snapshot.hasError) {
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.red),
);
} else {
var items = snapshot.data.items;
return ListView.builder(
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(items[index].name),
leading: CircleAvatar(
backgroundImage:
NetworkImage(items[index].owner.avatarUrl),
),
subtitle: Text(items[index].description),
);
},
);
}
}
}));
}
}

Flutter/Dart Error - NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'map' with matching arguments

I'm receiving an Error following the Json Deserialisation cookbook
NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'map' with matching arguments.
Bundle Class
class Bundle {
String resourceType;
String id;
String type;
int total;
List<Link> link;
List<Entry> entry;
Bundle(
{this.resourceType,
this.id,
this.type,
this.total,
this.link,
this.entry});
factory Bundle.fromJson(Map<String, dynamic> json) {
return Bundle(
resourceType : json['resourceType'],
id : json['id'],
type : json['type'],
total : json['total'],
);
}
Code:
try {
await parsePerson(resultString);
} catch (e) {
print('Bundlelist Error: $e');
}
Future<List<Bundle>> parsePerson(String body) async {
List<Bundle> bundleList = [];
try {
final parsed = json.decode(body);
bundleList = parsed.map<Bundle>((json) => Bundle.fromJson(json)).toList;
} catch (e) {
print('FutureError: $e');
}
return bundleList;
}
My Result string (partial): Full json is here.
{"resourceType":"Bundle","id":"f26779b4-5c3c-4c52-83b4-c689516a6a08","type":"searchset","link":[{"relation":"self","url":"https://fhir-open.sandboxcerner.com/dstu2/0b8a0111-e8e6-4c26-a91c-5069cbc6b1ca/Patient?name=b\u0026_count=20"},{"relation":"next","url":"https://fhir-open.sandboxcerner.com/dstu2/0b8a0111-e8e6-4c26-a91c-5069cbc6b1ca/Patient?-pageContext=7018d2bc-6be4-48e1-b8a4-a40f4e98c98c\u0026-pageDirection=NEXT"}],"entry":[{"fullUrl":"https://fhir-open.sandboxcerner.com/dstu2/0b8a0111-e8e6-4c26-a91c-5069cbc6b1ca/Patient/6160015","resource":{"resourceType":"Patient","id":"6160015","meta":{"versionId":"0","lastUpdated":"2019-07-08T20:37:03.000Z"},"text":{"status":"generated","div":"\u003Cdiv\u003E\u003Cp\u003E\u003Cb\u003EPatient\u003C/b\u003E\u003C/p\u003E\u003Cp\u003E\u003Cb\u003EName\u003C/b\u003E: 111d3fcaffb244b2b207c07ffa5a14, bf607a7f1f284e8aa3559d52249bc7\u003C/p\u003E\u003Cp\u003E\u003Cb\u003EDOB\u003C/b\u003E: Mar 15, 1936\u003C/p\u003E\u003Cp\u003E\u003Cb\u003EAdministrative Gend
I've tried various suggestions from here including:
final parsed = jsonDecode(body).cast<Map<String, dynamic>>();
Returns
NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments.
I'm quite lost as to what to try next.
You need an array, but your response is a map
You json string is too long, I can not paste full code contains your full json
You can copy paste , replace yourjsonstring and run full code below
Your json string contains control character \n and need to replace before parse
You can get all related class in full code
code snippet
String jsonString = '''yourjsonstring''';
String replaced = jsonString.replaceAll('\n',r'\\n');
final payload = payloadFromJson(replaced);
print(payload.link[0].relation);
print(payload.link[0].url);
print(payload.entry[0].resource.address[0].text);
full code
import 'package:flutter/material.dart';
// To parse this JSON data, do
//
// final payload = payloadFromJson(jsonString);
import 'dart:convert';
Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));
String payloadToJson(Payload data) => json.encode(data.toJson());
class Payload {
String resourceType;
String id;
String type;
List<Link> link;
List<Entry> entry;
Payload({
this.resourceType,
this.id,
this.type,
this.link,
this.entry,
});
factory Payload.fromJson(Map<String, dynamic> json) => Payload(
resourceType: json["resourceType"],
id: json["id"],
type: json["type"],
link: List<Link>.from(json["link"].map((x) => Link.fromJson(x))),
entry: List<Entry>.from(json["entry"].map((x) => Entry.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"resourceType": resourceType,
"id": id,
"type": type,
"link": List<dynamic>.from(link.map((x) => x.toJson())),
"entry": List<dynamic>.from(entry.map((x) => x.toJson())),
};
}
class Entry {
String fullUrl;
Resource resource;
Entry({
this.fullUrl,
this.resource,
});
factory Entry.fromJson(Map<String, dynamic> json) => Entry(
fullUrl: json["fullUrl"],
resource: Resource.fromJson(json["resource"]),
);
Map<String, dynamic> toJson() => {
"fullUrl": fullUrl,
"resource": resource.toJson(),
};
}
class Resource {
ResourceType resourceType;
String id;
Meta meta;
TextClass text;
List<Identifier> identifier;
bool active;
List<Name> name;
List<Telecom> telecom;
Gender gender;
DateTime birthDate;
List<Address> address;
Resource({
this.resourceType,
this.id,
this.meta,
this.text,
this.identifier,
this.active,
this.name,
this.telecom,
this.gender,
this.birthDate,
this.address,
});
factory Resource.fromJson(Map<String, dynamic> json) => Resource(
resourceType: resourceTypeValues.map[json["resourceType"]],
id: json["id"],
meta: Meta.fromJson(json["meta"]),
text: TextClass.fromJson(json["text"]),
identifier: List<Identifier>.from(json["identifier"].map((x) => Identifier.fromJson(x))),
active: json["active"],
name: List<Name>.from(json["name"].map((x) => Name.fromJson(x))),
telecom: List<Telecom>.from(json["telecom"].map((x) => Telecom.fromJson(x))),
gender: genderValues.map[json["gender"]],
birthDate: DateTime.parse(json["birthDate"]),
address: List<Address>.from(json["address"].map((x) => Address.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"resourceType": resourceTypeValues.reverse[resourceType],
"id": id,
"meta": meta.toJson(),
"text": text.toJson(),
"identifier": List<dynamic>.from(identifier.map((x) => x.toJson())),
"active": active,
"name": List<dynamic>.from(name.map((x) => x.toJson())),
"telecom": List<dynamic>.from(telecom.map((x) => x.toJson())),
"gender": genderValues.reverse[gender],
"birthDate": "${birthDate.year.toString().padLeft(4, '0')}-${birthDate.month.toString().padLeft(2, '0')}-${birthDate.day.toString().padLeft(2, '0')}",
"address": List<dynamic>.from(address.map((x) => x.toJson())),
};
}
class Address {
AddressUse use;
String text;
List<String> line;
String city;
String state;
String postalCode;
String country;
AddressPeriod period;
Address({
this.use,
this.text,
this.line,
this.city,
this.state,
this.postalCode,
this.country,
this.period,
});
factory Address.fromJson(Map<String, dynamic> json) => Address(
use: addressUseValues.map[json["use"]],
text: json["text"],
line: List<String>.from(json["line"].map((x) => x)),
city: json["city"],
state: json["state"],
postalCode: json["postalCode"],
country: json["country"],
period: json["period"] == null ? null : AddressPeriod.fromJson(json["period"]),
);
Map<String, dynamic> toJson() => {
"use": addressUseValues.reverse[use],
"text": text,
"line": List<dynamic>.from(line.map((x) => x)),
"city": city,
"state": state,
"postalCode": postalCode,
"country": country,
"period": period == null ? null : period.toJson(),
};
}
class AddressPeriod {
DateTime start;
AddressPeriod({
this.start,
});
factory AddressPeriod.fromJson(Map<String, dynamic> json) => AddressPeriod(
start: DateTime.parse(json["start"]),
);
Map<String, dynamic> toJson() => {
"start": start.toIso8601String(),
};
}
enum AddressUse { HOME, WORK, MOBILE }
final addressUseValues = EnumValues({
"home": AddressUse.HOME,
"mobile": AddressUse.MOBILE,
"work": AddressUse.WORK
});
enum Gender { UNKNOWN, OTHER }
final genderValues = EnumValues({
"other": Gender.OTHER,
"unknown": Gender.UNKNOWN
});
class Identifier {
IdentifierUse use;
Type type;
IdentifierSystem system;
String identifierValue;
Value value;
AddressPeriod period;
Identifier({
this.use,
this.type,
this.system,
this.identifierValue,
this.value,
this.period,
});
factory Identifier.fromJson(Map<String, dynamic> json) => Identifier(
use: identifierUseValues.map[json["use"]],
type: Type.fromJson(json["type"]),
system: identifierSystemValues.map[json["system"]],
identifierValue: json["value"],
value: Value.fromJson(json["_value"]),
period: AddressPeriod.fromJson(json["period"]),
);
Map<String, dynamic> toJson() => {
"use": identifierUseValues.reverse[use],
"type": type.toJson(),
"system": identifierSystemValues.reverse[system],
"value": identifierValue,
"_value": value.toJson(),
"period": period.toJson(),
};
}
enum IdentifierSystem { URN_OID_2168401113883378700, URN_OID_111111, URN_OID_21684011138833421000110000112 }
final identifierSystemValues = EnumValues({
"urn:oid:1.1.1.1.1.1": IdentifierSystem.URN_OID_111111,
"urn:oid:2.16.840.1.113883.3.42.10001.100001.12": IdentifierSystem.URN_OID_21684011138833421000110000112,
"urn:oid:2.16.840.1.113883.3.787.0.0": IdentifierSystem.URN_OID_2168401113883378700
});
class Type {
List<Coding> coding;
TextEnum text;
Type({
this.coding,
this.text,
});
factory Type.fromJson(Map<String, dynamic> json) => Type(
coding: json["coding"] == null ? null : List<Coding>.from(json["coding"].map((x) => Coding.fromJson(x))),
text: textEnumValues.map[json["text"]],
);
Map<String, dynamic> toJson() => {
"coding": coding == null ? null : List<dynamic>.from(coding.map((x) => x.toJson())),
"text": textEnumValues.reverse[text],
};
}
class Coding {
String system;
Code code;
Display display;
bool userSelected;
Coding({
this.system,
this.code,
this.display,
this.userSelected,
});
factory Coding.fromJson(Map<String, dynamic> json) => Coding(
system: json["system"],
code: codeValues.map[json["code"]],
display: displayValues.map[json["display"]],
userSelected: json["userSelected"],
);
Map<String, dynamic> toJson() => {
"system": system,
"code": codeValues.reverse[code],
"display": displayValues.reverse[display],
"userSelected": userSelected,
};
}
enum Code { MR }
final codeValues = EnumValues({
"MR": Code.MR
});
enum Display { MEDICAL_RECORD_NUMBER }
final displayValues = EnumValues({
"Medical record number": Display.MEDICAL_RECORD_NUMBER
});
enum TextEnum { COMMUNITY_MEDICAL_RECORD_NUMBER, MRN, MILITARY_ID }
final textEnumValues = EnumValues({
"Community Medical Record Number": TextEnum.COMMUNITY_MEDICAL_RECORD_NUMBER,
"Military Id": TextEnum.MILITARY_ID,
"MRN": TextEnum.MRN
});
enum IdentifierUse { USUAL }
final identifierUseValues = EnumValues({
"usual": IdentifierUse.USUAL
});
class Value {
List<Extension> extension;
Value({
this.extension,
});
factory Value.fromJson(Map<String, dynamic> json) => Value(
extension: List<Extension>.from(json["extension"].map((x) => Extension.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"extension": List<dynamic>.from(extension.map((x) => x.toJson())),
};
}
class Extension {
String url;
String valueString;
Extension({
this.url,
this.valueString,
});
factory Extension.fromJson(Map<String, dynamic> json) => Extension(
url: json["url"],
valueString: json["valueString"],
);
Map<String, dynamic> toJson() => {
"url": url,
"valueString": valueString,
};
}
class Meta {
String versionId;
DateTime lastUpdated;
Meta({
this.versionId,
this.lastUpdated,
});
factory Meta.fromJson(Map<String, dynamic> json) => Meta(
versionId: json["versionId"],
lastUpdated: DateTime.parse(json["lastUpdated"]),
);
Map<String, dynamic> toJson() => {
"versionId": versionId,
"lastUpdated": lastUpdated.toIso8601String(),
};
}
class Name {
NameUse use;
String text;
List<String> family;
List<String> given;
List<String> prefix;
NamePeriod period;
List<String> suffix;
Name({
this.use,
this.text,
this.family,
this.given,
this.prefix,
this.period,
this.suffix,
});
factory Name.fromJson(Map<String, dynamic> json) => Name(
use: nameUseValues.map[json["use"]],
text: json["text"],
family: List<String>.from(json["family"].map((x) => x)),
given: List<String>.from(json["given"].map((x) => x)),
prefix: json["prefix"] == null ? null : List<String>.from(json["prefix"].map((x) => x)),
period: json["period"] == null ? null : NamePeriod.fromJson(json["period"]),
suffix: json["suffix"] == null ? null : List<String>.from(json["suffix"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"use": nameUseValues.reverse[use],
"text": text,
"family": List<dynamic>.from(family.map((x) => x)),
"given": List<dynamic>.from(given.map((x) => x)),
"prefix": prefix == null ? null : List<dynamic>.from(prefix.map((x) => x)),
"period": period == null ? null : period.toJson(),
"suffix": suffix == null ? null : List<dynamic>.from(suffix.map((x) => x)),
};
}
class NamePeriod {
DateTime start;
DateTime end;
NamePeriod({
this.start,
this.end,
});
factory NamePeriod.fromJson(Map<String, dynamic> json) => NamePeriod(
start: json["start"] == null ? null : DateTime.parse(json["start"]),
end: json["end"] == null ? null : DateTime.parse(json["end"]),
);
Map<String, dynamic> toJson() => {
"start": start == null ? null : start.toIso8601String(),
"end": end == null ? null : end.toIso8601String(),
};
}
enum NameUse { OFFICIAL, OLD }
final nameUseValues = EnumValues({
"official": NameUse.OFFICIAL,
"old": NameUse.OLD
});
enum ResourceType { PATIENT }
final resourceTypeValues = EnumValues({
"Patient": ResourceType.PATIENT
});
class Telecom {
TelecomSystem system;
ValueEnum value;
AddressUse use;
AddressPeriod period;
Telecom({
this.system,
this.value,
this.use,
this.period,
});
factory Telecom.fromJson(Map<String, dynamic> json) => Telecom(
system: telecomSystemValues.map[json["system"]],
value: valueEnumValues.map[json["value"]],
use: addressUseValues.map[json["use"]],
period: json["period"] == null ? null : AddressPeriod.fromJson(json["period"]),
);
Map<String, dynamic> toJson() => {
"system": telecomSystemValues.reverse[system],
"value": valueEnumValues.reverse[value],
"use": addressUseValues.reverse[use],
"period": period == null ? null : period.toJson(),
};
}
enum TelecomSystem { PHONE, EMAIL }
final telecomSystemValues = EnumValues({
"email": TelecomSystem.EMAIL,
"phone": TelecomSystem.PHONE
});
enum ValueEnum { THE_3213213213, NAME_FAKEMAIL_COM, THE_8888888888 }
final valueEnumValues = EnumValues({
"name#fakemail.com": ValueEnum.NAME_FAKEMAIL_COM,
"321-321-3213": ValueEnum.THE_3213213213,
"888-888-8888": ValueEnum.THE_8888888888
});
class TextClass {
Status status;
String div;
TextClass({
this.status,
this.div,
});
factory TextClass.fromJson(Map<String, dynamic> json) => TextClass(
status: statusValues.map[json["status"]],
div: json["div"],
);
Map<String, dynamic> toJson() => {
"status": statusValues.reverse[status],
"div": div,
};
}
enum Status { GENERATED }
final statusValues = EnumValues({
"generated": Status.GENERATED
});
class Link {
String relation;
String url;
Link({
this.relation,
this.url,
});
factory Link.fromJson(Map<String, dynamic> json) => Link(
relation: json["relation"],
url: json["url"],
);
Map<String, dynamic> toJson() => {
"relation": relation,
"url": url,
};
}
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String jsonString = '''yourjsonstring''';
//String jsonString = '''{"newLine": "here is a \n newline \u003E \u0026 \u003C aaa"}''';
void _incrementCounter() {
String replaced = jsonString.replaceAll('\n',r'\\n');
final payload = payloadFromJson(replaced);
print(payload.link[0].relation);
print(payload.link[0].url);
print(payload.entry[0].resource.address[0].text);
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

How to map a json with nested objects of same type in Flutter?

I need to map this json in Flutter, I tried with fromMap of JsonCodec but throws StackOverflowError. Any suggestions would be very welcome.
{
"items": [
{
"id": 59,
"name": "Trastornos de la glándula tiroides",
"parent": {
"id": 58,
"name": "Enfermedades endócrinas",
"parent": null,
"diagnosis_classifications": {
"count": 1,
"items": [
{
"id": 58,
"name": "CIE-10 Diagnósticos",
"code": "E00-E35"
}
]
}
},
"diagnosis_classifications": {
"count": 1,
"items": [
{
"id": 59,
"name": "CIE-10 Diagnósticos",
"code": "E00-E07"
}
]
}
}
]
}
You can pare with var payload = payloadFromJson(jsonString);
You can see long sample json string in full code
related class
// To parse this JSON data, do
//
// final payload = payloadFromJson(jsonString);
import 'dart:convert';
Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));
String payloadToJson(Payload data) => json.encode(data.toJson());
class Parent {
int id;
String name;
Payload parent;
DiagnosisClassifications diagnosisClassifications;
Parent({
this.id,
this.name,
this.parent,
this.diagnosisClassifications,
});
factory Parent.fromJson(Map<String, dynamic> json) => Parent(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
parent: json["parent"] == null ? null : Payload.fromJson(json["parent"]),
diagnosisClassifications: json["diagnosis_classifications"] == null ? null : DiagnosisClassifications.fromJson(json["diagnosis_classifications"]),
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"parent": parent == null ? null : parent.toJson(),
"diagnosis_classifications": diagnosisClassifications == null ? null : diagnosisClassifications.toJson(),
};
}
class Payload {
int id;
String name;
Parent parent;
DiagnosisClassifications diagnosisClassifications;
Payload({
this.id,
this.name,
this.parent,
this.diagnosisClassifications,
});
factory Payload.fromJson(Map<String, dynamic> json) => Payload(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
parent: json["parent"] == null ? null : Parent.fromJson(json["parent"]),
diagnosisClassifications: json["diagnosis_classifications"] == null ? null : DiagnosisClassifications.fromJson(json["diagnosis_classifications"]),
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"parent": parent == null ? null : parent.toJson(),
"diagnosis_classifications": diagnosisClassifications == null ? null : diagnosisClassifications.toJson(),
};
}
class DiagnosisClassifications {
int count;
List<Item> items;
DiagnosisClassifications({
this.count,
this.items,
});
factory DiagnosisClassifications.fromJson(Map<String, dynamic> json) => DiagnosisClassifications(
count: json["count"] == null ? null : json["count"],
items: json["items"] == null ? null : List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"count": count == null ? null : count,
"items": items == null ? null : List<dynamic>.from(items.map((x) => x.toJson())),
};
}
class Item {
int id;
String name;
String code;
Item({
this.id,
this.name,
this.code,
});
factory Item.fromJson(Map<String, dynamic> json) => Item(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
code: json["code"] == null ? null : json["code"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"code": code == null ? null : code,
};
}
full code
import 'package:flutter/material.dart';
// To parse this JSON data, do
//
// final payload = payloadFromJson(jsonString);
import 'dart:convert';
Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));
String payloadToJson(Payload data) => json.encode(data.toJson());
class Parent {
int id;
String name;
Payload parent;
DiagnosisClassifications diagnosisClassifications;
Parent({
this.id,
this.name,
this.parent,
this.diagnosisClassifications,
});
factory Parent.fromJson(Map<String, dynamic> json) => Parent(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
parent: json["parent"] == null ? null : Payload.fromJson(json["parent"]),
diagnosisClassifications: json["diagnosis_classifications"] == null ? null : DiagnosisClassifications.fromJson(json["diagnosis_classifications"]),
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"parent": parent == null ? null : parent.toJson(),
"diagnosis_classifications": diagnosisClassifications == null ? null : diagnosisClassifications.toJson(),
};
}
class Payload {
int id;
String name;
Parent parent;
DiagnosisClassifications diagnosisClassifications;
Payload({
this.id,
this.name,
this.parent,
this.diagnosisClassifications,
});
factory Payload.fromJson(Map<String, dynamic> json) => Payload(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
parent: json["parent"] == null ? null : Parent.fromJson(json["parent"]),
diagnosisClassifications: json["diagnosis_classifications"] == null ? null : DiagnosisClassifications.fromJson(json["diagnosis_classifications"]),
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"parent": parent == null ? null : parent.toJson(),
"diagnosis_classifications": diagnosisClassifications == null ? null : diagnosisClassifications.toJson(),
};
}
class DiagnosisClassifications {
int count;
List<Item> items;
DiagnosisClassifications({
this.count,
this.items,
});
factory DiagnosisClassifications.fromJson(Map<String, dynamic> json) => DiagnosisClassifications(
count: json["count"] == null ? null : json["count"],
items: json["items"] == null ? null : List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"count": count == null ? null : count,
"items": items == null ? null : List<dynamic>.from(items.map((x) => x.toJson())),
};
}
class Item {
int id;
String name;
String code;
Item({
this.id,
this.name,
this.code,
});
factory Item.fromJson(Map<String, dynamic> json) => Item(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
code: json["code"] == null ? null : json["code"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"code": code == null ? null : code,
};
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String jsonStr = ''' {
"id": 59,
"name": "Trastornos de la glándula tiroides",
"parent": {
"id": 58,
"name": "Enfermedades endócrinas",
"parent" : {
"id": 60,
"name": "Enfermedades endócrinas",
"parent" : null,
"diagnosis_classifications": {
"count": 1,
"items": [
{
"id": 60,
"name": "CIE-10 Diagnósticos",
"code": "E00-E35"
}
]
}
},
"diagnosis_classifications": {
"count": 1,
"items": [
{
"id": 58,
"name": "CIE-10 Diagnósticos",
"code": "E00-E35"
}
]
}
}
} ''';
void _incrementCounter() {
final payload = payloadFromJson(jsonStr);
print('payload.id ${payload.id}');
print('payload.parent.id ${payload.parent.id}');
print('payload.parent.parent.id ${payload.parent.parent.id}');
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
#override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
output
I/flutter ( 8924): payload.id 59
I/flutter ( 8924): payload.parent.id 58
I/flutter ( 8924): payload.parent.parent.id 60