我是flutter的新手,我試圖在listview中獲取我的資料。我按照這個鏈接--
https://flutter.dev/docs/cookbook/networking/fetch-data#complete-example
但不知道問題出在哪里。我也在嘗試這種方式。
這里是我的完整代碼
import 'dart:convert';
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
Future<List> listData() async {
final token =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9. eyJzdWIiOjI4OTksImlzcyI6Imh0dHBzOi8vcG9ydGFsLWFwaS5qb21ha2hhdGEuY29tL2FwaS9hdXRoL2xvZ2luIwiaWF0IjoxNjI5NDM2MzA1LCJleHAiOjE2Mjk1MjI3MDUsIm5iZiI6MTYyOTQzNjMwNSwianRpIjoiVWY5cEFBcVnSG1ZdWlTMyJ9. BAjwiHMNozDWDHHrCP1p7Mt_dsNo_KNYWMQmOCuFhwQ'/span>。
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'/span>, 'orderType'/span>: '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。
標題。'Flutter'。
主題。ThemeData(
primarySwatch: Colors.blue,
),
主頁。MyHomePage(title: 'List View')。
);
}
}
class MyHomePage extends StatefulWidget{
MyHomePage({Key? key, required this.title}) : super(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(
身體。中心(
孩子。FutureBuilder<List> (
future: futureAlbum,
建設者。(context, snapshot) {
if ( snapshot.hasData) {
print(快照.資料)。
return Text(snapshot.data!.fullName)。
} else if (snapshot.hasError) {
return Text('${snapshot.error}') 。
}
return const CircularProgressIndicator()。
},
),
),
);
}
}
這是錯誤的
。
這是在postman中的json回應
。{
"success": true。
"data": {
"count": 259,
"data": [
{
"idEmployee": 3559,
"avatar": "f8b8ad832a591db9c86a157a3739d98b.jpg",
"fullName": "A X C"。
"officeID": "1003559",
"email": "",
"designation": "客戶經理",
"部門": "Accounts",
"mobileNumber": "",
"workStation": "軟體辦公室"。
"businessUnit": "EMD".
}
],
"query": [
{
"query": "選擇SQL_CALC_FOUND_ROWS hrEmp.employee_id作為idEmployee, `hrEmp`.`avatar`作為`avatar`, `hrEmp`.`full_name`作為`fullName`, `hrEmp`. `employee_custom_id`作為`officeID`, `hr_organization_setup`.`off_email`作為`email`, `hr_designation_master`.`designation_title`作為`designation`, `hr_departments`.`department`作為`department`, `hr_organization_setup`. `off_number`作為`mobileNumber`, `hr_work_station`.`work_station_name`作為`workStation`, `projects`.`project_name`作為`businessUnit`從`hr_employee`作為`hrEmp`內部加入`hr_organization_setup`在`hr_organization_setup`. `employee_id` = `hrEmp`.`employee_id` 行內`projects` on `projects`.`id_projects` = `hr_organization_setup`. `id_business_unit`內接`hr_designation_master` on `hr_designation_master`.`designation_id` = `hr_organization_setup`.`employee_desig_id`內接`hr_departments` on `hr_departments`.`id_department` = `hr_organization_setup`.`id_department`左接`hr_work_station` on `hr_organization_setup`.`work_station_id` =`hr_work_station`. 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"/span>,
"bindings": [
"激活",
"激活"。
"作業",
"JV"。
"激活"。
"激活"。
"activated".
],
"時間"。7.19。
}
]
},
"id": 2899 "id".
}
我只需要這些資料顯示在listview中
。 "count"/span>: 259,
"data": [
{
"idEmployee": 3559,
"avatar": "f8b8ad832a591db9c86a157a3739d98b.jpg",
"fullName": "A X C"。
"officeID": "1003559",
"email": "",
"designation": "客戶經理",
"部門": "Accounts",
"mobileNumber": "",
"workStation": "軟體辦公室"。
"businessUnit": "EMD".
}
],
這就是Android Studio運行控制臺
。
uj5u.com熱心網友回復:
這里有太多的data標簽,我想這可能會引起混淆。首先,response.data是整個回應,就像你從Postman輸出發布的那樣。然后,在回應中還有一個data,以及另外一個data。這最后一個是一個陣列,你只需要它的第一個元素。
簡單地替換這一行:
return List.fromJson(jsonDecode(response.data))。
用這個:
return List. fromJson(response.data["data"]["data"][0] )。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/315714.html
標籤:
上一篇:我的節點和npm已經是最新版本
