I am new in flutter, i tried to get my data in listview. i followed this link -
https://flutter.dev/docs/cookbook/networking/fetch-data#complete-example
but don't know what is the problem. also i'am trying this way.
here is my full code
import 'dart:convert';
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
Future<List> listData() async {
final token =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjI4OTksImlzcyI6Imh0dHBzOi8vcG9ydGFsLWFwaS5qb21ha2hhdGEuY29tL2FwaS9hdXRoL2xvZ2luIiwiaWF0IjoxNjI5NDM2MzA1LCJleHAiOjE2Mjk1MjI3MDUsIm5iZiI6MTYyOTQzNjMwNSwianRpIjoiVWY5cEFBcVVnSG1ZdWlTMyJ9.BAjwiHMNozDWDHHrCP1p7Mt_dsNo_KNYWMQmOCuFhwQ';
String url =
'https://portal-api.jomakhata.com/api/getOrganizationData?token=${token}';
Dio dio = new Dio();
dio.options.headers['Content-Type'] = 'application/json';
final body = {'limit': 1, 'orderBy': 'idEmployee', 'orderType': 'DESC'};
final response = await dio.post(url, data: body);
if (response.statusCode == 200) {
print(response.statusCode);
// print(response.data);
return List.fromJson(jsonDecode(response.data));
} else {
throw Exception('Failed!');
}
}
class List {
final int idEmployee;
final String fullName;
List({
required this.idEmployee,
required this.fullName,
});
factory List.fromJson(Map<String, dynamic> json) {
return List(
idEmployee: json['idEmployee'],
fullName: json['fullName'],
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'List View'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Future<List> futureAlbum;
@override
void initState() {
super.initState();
futureAlbum = listData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder<List>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
print(snapshot.data);
return Text(snapshot.data!.fullName);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
),
),
);
}
}
This is Error
This is json response in postman
{
"success": true,
"data": {
"count": 259,
"data": [
{
"idEmployee": 3559,
"avatar": "f8b8ad832a591db9c86a157a3739d98b.jpg",
"fullName": "A X C",
"officeID": "1003559",
"email": "",
"designation": "Account Manager",
"department": "Accounts",
"mobileNumber": "",
"workStation": "Software Office",
"businessUnit": "EMD"
}
],
"query": [
{
"query": "select SQL_CALC_FOUND_ROWS hrEmp.employee_id AS idEmployee, `hrEmp`.`avatar` as `avatar`, `hrEmp`.`full_name` as `fullName`, `hrEmp`.`employee_custom_id` as `officeID`, `hr_organization_setup`.`off_email` as `email`, `hr_designation_master`.`designation_title` as `designation`, `hr_departments`.`department` as `department`, `hr_organization_setup`.`off_number` as `mobileNumber`, `hr_work_station`.`work_station_name` as `workStation`, `projects`.`project_name` as `businessUnit` from `hr_employee` as `hrEmp` inner join `hr_organization_setup` on `hr_organization_setup`.`employee_id` = `hrEmp`.`employee_id` inner join `projects` on `projects`.`id_projects` = `hr_organization_setup`.`id_business_unit` inner join `hr_designation_master` on `hr_designation_master`.`designation_id` = `hr_organization_setup`.`employee_desig_id` inner join `hr_departments` on `hr_departments`.`id_department` = `hr_organization_setup`.`id_department` left join `hr_work_station` on `hr_organization_setup`.`work_station_id` = `hr_work_station`.`work_station_id` where `hrEmp`.`publication_status` = ? and `hr_organization_setup`.`publication_status` = ? and `hr_organization_setup`.`working_status` in (?, ?) and `hr_designation_master`.`publication_status` = ? and `hr_departments`.`publication_status` = ? and `hr_work_station`.`publication_status` = ? order by `idEmployee` desc limit 1 offset 0",
"bindings": [
"activated",
"activated",
"Working",
"JV",
"activated",
"activated",
"activated"
],
"time": 7.19
}
]
},
"id": 2899
}
i need just these data to show in listview
"count": 259,
"data": [
{
"idEmployee": 3559,
"avatar": "f8b8ad832a591db9c86a157a3739d98b.jpg",
"fullName": "A X C",
"officeID": "1003559",
"email": "",
"designation": "Account Manager",
"department": "Accounts",
"mobileNumber": "",
"workStation": "Software Office",
"businessUnit": "EMD"
}
],
And This is Android Studio Run Console
There are too many data
tags here, I think that may be confusing. First, response.data
is the entire response, like you posted from Postman output. Then there is a data
within the response, and one more data
within. This last one is an array, and you need only the first element of it.
Simply replace this line:
return List.fromJson(jsonDecode(response.data));
with this:
return List.fromJson(response.data["data"]["data"][0]);