Parsing nested Json
Last updated
Last updated
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class Job {
final String position;
final String company;
final String description;
Job({this.position, this.company, this.description});
factory Job.fromJson(Map<String, dynamic> json) {
return Job(
position: json['nama'],
company: json['kontingen'],
description: json['kelamin'],
);
}
}
class JobsListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Container(
child: Text('ok'),
),
Expanded(
child: FutureBuilder<List<Job>>(
future: _fetchJobs(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Job> data = snapshot.data;
return _jobsListView(data);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
),
),
],
),
),
);
}
Future<List<Job>> _fetchJobs() async {
final jobsListAPIUrl = 'http://dwirudi.com/skc/public/list_peserta';
// final jobsListAPIUrl = 'https://mock-json-service.glitch.me/';
final response = await http.get(jobsListAPIUrl);
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body)['data'];
return jsonResponse.map((job) => new Job.fromJson(job)).toList();
} else {
throw Exception('Failed to load jobs from API');
}
}
ListView _jobsListView(data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
return _tile(data[index].position, data[index].company, Icons.work);
});
}
ListTile _tile(String title, String subtitle, IconData icon) => ListTile(
title: Text(title,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20,
)),
subtitle: Text(subtitle),
leading: Icon(
icon,
color: Colors.blue[500],
),
);
}