JAVASCRIPT
flutter response to json
dependencies:
http: <latest_version>
Future<http.Response> fetchPhotos(http.Client client) async {
return client.get(Uri.parse('https://jsonplaceholder.typicode.com/photos'));
}
class Photo {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;
const Photo({
required this.albumId,
required this.id,
required this.title,
required this.url,
required this.thumbnailUrl,
});
factory Photo.fromJson(Map<String, dynamic> json) {
return Photo(
albumId: json['albumId'] as int,
id: json['id'] as int,
title: json['title'] as String,
url: json['url'] as String,
thumbnailUrl: json['thumbnailUrl'] as String,
);
}
}
// A function that converts a response body into a List<Photo>.
List<Photo> parsePhotos(String responseBody) {
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}
Future<List<Photo>> fetchPhotos(http.Client client) async {
final response = await client
.get(Uri.parse('https://jsonplaceholder.typicode.com/photos'));
// Use the compute function to run parsePhotos in a separate isolate.
return parsePhotos(response.body);
}
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Photo>> fetchPhotos(http.Client client) async {
final response = await client
.get(Uri.parse('https://jsonplaceholder.typicode.com/photos'));
// Use the compute function to run parsePhotos in a separate isolate.
return compute(parsePhotos, response.body);
}
// A function that converts a response body into a List<Photo>.
List<Photo> parsePhotos(String responseBody) {
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}
class Photo {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;
const Photo({
required this.albumId,
required this.id,
required this.title,
required this.url,
required this.thumbnailUrl,
});
factory Photo.fromJson(Map<String, dynamic> json) {
return Photo(
albumId: json['albumId'] as int,
id: json['id'] as int,
title: json['title'] as String,
url: json['url'] as String,
thumbnailUrl: json['thumbnailUrl'] as String,
);
}
}
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const appTitle = 'Isolate Demo';
return const MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: FutureBuilder<List<Photo>>(
future: fetchPhotos(http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Center(
child: Text('An error has occurred!'),
);
} else if (snapshot.hasData) {
return PhotosList(photos: snapshot.data!);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
}
class PhotosList extends StatelessWidget {
const PhotosList({super.key, required this.photos});
final List<Photo> photos;
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: photos.length,
itemBuilder: (context, index) {
return Image.network(photos[index].thumbnailUrl);
},
);
}
}
Json response to dart
///
/// Code generated by jsonToDartModel http://json.fluttergeeks.in/
///
class SomeRootEntityMultidimensional {
/*
{
"name": "y"
}
*/
String name;
SomeRootEntityMultidimensional({
this.name,
});
SomeRootEntityMultidimensional.fromJson(Map<String, dynamic> json) {
name = json['name']?.toString();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
return data;
}
}
class SomeRootEntityAnObjList {
/*
{
"name": "y"
}
*/
String name;
SomeRootEntityAnObjList({
this.name,
});
SomeRootEntityAnObjList.fromJson(Map<String, dynamic> json) {
name = json['name']?.toString();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
return data;
}
}
class SomeRootEntityAnObj {
/*
{
"name": "x",
"age": 18.1
}
*/
String name;
double age;
SomeRootEntityAnObj({
this.name,
this.age,
});
SomeRootEntityAnObj.fromJson(Map<String, dynamic> json) {
name = json['name']?.toString();
age = json['age']?.toDouble();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
data['age'] = age;
return data;
}
}
class SomeRootEntity {
/*
{
"some_snake_case_prop": "",
"anInt": 1,
"aDouble": 2.3,
"aString": "hello",
"aBool": false,
"anObj": {
"name": "x",
"age": 18.1
},
"anObjList": [
{
"name": "y"
}
],
"aStrList": [
"something"
],
"multidimensional": [
[
[
{
"name": "y"
}
]
]
]
}
*/
String someSnakeCaseProp;
int anInt;
double aDouble;
String aString;
bool aBool;
SomeRootEntityAnObj anObj;
List<SomeRootEntityAnObjList> anObjList;
List<String> aStrList;
List<List<List<SomeRootEntityMultidimensional>>> multidimensional;
SomeRootEntity({
this.someSnakeCaseProp,
this.anInt,
this.aDouble,
this.aString,
this.aBool,
this.anObj,
this.anObjList,
this.aStrList,
this.multidimensional,
});
SomeRootEntity.fromJson(Map<String, dynamic> json) {
someSnakeCaseProp = json['some_snake_case_prop']?.toString();
anInt = json['anInt']?.toInt();
aDouble = json['aDouble']?.toDouble();
aString = json['aString']?.toString();
aBool = json['aBool'];
anObj = (json['anObj'] != null) ? SomeRootEntityAnObj.fromJson(json['anObj']) : null;
if (json['anObjList'] != null) {
final v = json['anObjList'];
final arr0 = <SomeRootEntityAnObjList>[];
v.forEach((v) {
arr0.add(SomeRootEntityAnObjList.fromJson(v));
});
anObjList = arr0;
}
if (json['aStrList'] != null) {
final v = json['aStrList'];
final arr0 = <String>[];
v.forEach((v) {
arr0.add(v.toString());
});
aStrList = arr0;
}
if (json['multidimensional'] != null) {
final v = json['multidimensional'];
final arr0 = <List<List<SomeRootEntityMultidimensional>>>[];
v.forEach((v) {
final arr1 = <List<SomeRootEntityMultidimensional>>[];
v.forEach((v) {
final arr2 = <SomeRootEntityMultidimensional>[];
v.forEach((v) {
arr2.add(SomeRootEntityMultidimensional.fromJson(v));
});
arr1.add(arr2);
});
arr0.add(arr1);
});
multidimensional = arr0;
}
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['some_snake_case_prop'] = someSnakeCaseProp;
data['anInt'] = anInt;
data['aDouble'] = aDouble;
data['aString'] = aString;
data['aBool'] = aBool;
if (anObj != null) {
data['anObj'] = anObj.toJson();
}
if (anObjList != null) {
final v = anObjList;
final arr0 = [];
v.forEach((v) {
arr0.add(v.toJson());
});
data['anObjList'] = arr0;
}
if (aStrList != null) {
final v = aStrList;
final arr0 = [];
v.forEach((v) {
arr0.add(v);
});
data['aStrList'] = arr0;
}
if (multidimensional != null) {
final v = multidimensional;
final arr0 = [];
v.forEach((v) {
final arr1 = [];
v.forEach((v) {
final arr2 = [];
v.forEach((v) {
arr2.add(v.toJson());
});
arr1.add(arr2);
});
arr0.add(arr1);
});
data['multidimensional'] = arr0;
}
return data;
}
}
Json response to dart
///
/// Code generated by jsonToDartModel http://json.fluttergeeks.in/
///
class SomeRootEntityMultidimensional {
/*
{
"name": "y"
}
*/
String name;
SomeRootEntityMultidimensional({
this.name,
});
SomeRootEntityMultidimensional.fromJson(Map<String, dynamic> json) {
name = json['name']?.toString();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
return data;
}
}
class SomeRootEntityAnObjList {
/*
{
"name": "y"
}
*/
String name;
SomeRootEntityAnObjList({
this.name,
});
SomeRootEntityAnObjList.fromJson(Map<String, dynamic> json) {
name = json['name']?.toString();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
return data;
}
}
class SomeRootEntityAnObj {
/*
{
"name": "x",
"age": 18.1
}
*/
String name;
double age;
SomeRootEntityAnObj({
this.name,
this.age,
});
SomeRootEntityAnObj.fromJson(Map<String, dynamic> json) {
name = json['name']?.toString();
age = json['age']?.toDouble();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
data['age'] = age;
return data;
}
}
class SomeRootEntity {
/*
{
"some_snake_case_prop": "",
"anInt": 1,
"aDouble": 2.3,
"aString": "hello",
"aBool": false,
"anObj": {
"name": "x",
"age": 18.1
},
"anObjList": [
{
"name": "y"
}
],
"aStrList": [
"something"
],
"multidimensional": [
[
[
{
"name": "y"
}
]
]
]
}
*/
String someSnakeCaseProp;
int anInt;
double aDouble;
String aString;
bool aBool;
SomeRootEntityAnObj anObj;
List<SomeRootEntityAnObjList> anObjList;
List<String> aStrList;
List<List<List<SomeRootEntityMultidimensional>>> multidimensional;
SomeRootEntity({
this.someSnakeCaseProp,
this.anInt,
this.aDouble,
this.aString,
this.aBool,
this.anObj,
this.anObjList,
this.aStrList,
this.multidimensional,
});
SomeRootEntity.fromJson(Map<String, dynamic> json) {
someSnakeCaseProp = json['some_snake_case_prop']?.toString();
anInt = json['anInt']?.toInt();
aDouble = json['aDouble']?.toDouble();
aString = json['aString']?.toString();
aBool = json['aBool'];
anObj = (json['anObj'] != null) ? SomeRootEntityAnObj.fromJson(json['anObj']) : null;
if (json['anObjList'] != null) {
final v = json['anObjList'];
final arr0 = <SomeRootEntityAnObjList>[];
v.forEach((v) {
arr0.add(SomeRootEntityAnObjList.fromJson(v));
});
anObjList = arr0;
}
if (json['aStrList'] != null) {
final v = json['aStrList'];
final arr0 = <String>[];
v.forEach((v) {
arr0.add(v.toString());
});
aStrList = arr0;
}
if (json['multidimensional'] != null) {
final v = json['multidimensional'];
final arr0 = <List<List<SomeRootEntityMultidimensional>>>[];
v.forEach((v) {
final arr1 = <List<SomeRootEntityMultidimensional>>[];
v.forEach((v) {
final arr2 = <SomeRootEntityMultidimensional>[];
v.forEach((v) {
arr2.add(SomeRootEntityMultidimensional.fromJson(v));
});
arr1.add(arr2);
});
arr0.add(arr1);
});
multidimensional = arr0;
}
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['some_snake_case_prop'] = someSnakeCaseProp;
data['anInt'] = anInt;
data['aDouble'] = aDouble;
data['aString'] = aString;
data['aBool'] = aBool;
if (anObj != null) {
data['anObj'] = anObj.toJson();
}
if (anObjList != null) {
final v = anObjList;
final arr0 = [];
v.forEach((v) {
arr0.add(v.toJson());
});
data['anObjList'] = arr0;
}
if (aStrList != null) {
final v = aStrList;
final arr0 = [];
v.forEach((v) {
arr0.add(v);
});
data['aStrList'] = arr0;
}
if (multidimensional != null) {
final v = multidimensional;
final arr0 = [];
v.forEach((v) {
final arr1 = [];
v.forEach((v) {
final arr2 = [];
v.forEach((v) {
arr2.add(v.toJson());
});
arr1.add(arr2);
});
arr0.add(arr1);
});
data['multidimensional'] = arr0;
}
return data;
}
}