You can copy paste run full code below
Step 1: parse with List<Payload> payloadList = payloadFromJson(jsonString);
, you can see full code for detail Payload
class definition
Step 2: For loop like this
payloadList.forEach((element) {
print(element.name);
print(element.id);
});
output
I/flutter (17660): lakshay
I/flutter (17660): 1217
I/flutter (17660): lakshay
I/flutter (17660): 1217
full code
import 'package:flutter/material.dart';
import 'dart:convert';
List<Payload> payloadFromJson(String str) =>
List<Payload>.from(json.decode(str).map((x) => Payload.fromJson(x)));
String payloadToJson(List<Payload> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Payload {
Payload({
this.name,
this.id,
this.type,
this.temperature,
this.date,
});
String name;
String id;
String type;
double temperature;
String date;
factory Payload.fromJson(Map<String, dynamic> json) => Payload(
name: json["name"],
id: json["id"],
type: json["type"],
temperature: json["temperature"].toDouble(),
date: json["date"],
);
Map<String, dynamic> toJson() => {
"name": name,
"id": id,
"type": type,
"temperature": temperature,
"date": date,
};
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
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 = '''
[
{"name":"lakshay","id":"1217","type":"Staff","temperature":56.6,"date":"12-06-2020"},
{"name":"lakshay","id":"1217","type":"Staff","temperature":56.6,"date":"12-06-2020"}
]
''';
void _incrementCounter() {
List<Payload> payloadList = payloadFromJson(jsonString);
payloadList.forEach((element) {
print(element.name);
print(element.id);
});
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.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}