我要在应用程序中使用服务器中的数据。为此,我将使用本地服务器上的json接收数据,但问题是数据为空,并且我的代码未提取该数据。
这是代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp( MaterialApp(
home: MyHomePage(),
));
}
class MyHomePage extends StatefulWidget {
var title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<Base> _getBase() async {
var data = await http.get(Uri.encodeFull("http://192.168.1.13:5000/json"));
var jsonData = json.decode(data.body);
Base base = Base.fromJson(jsonData);
return base;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hi'),
),
body: Container(
child: FutureBuilder(
future: _getBase(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.teal)),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.categories.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
subtitle: Text(snapshot.data.categories[index].category_id),
title: Text(snapshot.data.categories[index].devices[index].title),
leading: CircleAvatar(
// ignore: argument_type_not_assignable
// backgroundImage: NetworkImage(snapshot.data.devices[index].thumbnailUrl),
)
);
},
);
}
},
),
),
);
}
}
这是用于json的数据模型:
class Base{
List <Category> array;
List<Category> categories;
Base( {this.categories});
factory Base.fromJson(Map<String,dynamic> parsedJson){
var list = parsedJson['categories'] as List;
List<Category> categoryList = list.map((i) => Category.fromJson(i)).toList();
return Base(
categories: categoryList
);
}
}
class Category {
int category_id,device_type_id,room_id;
List<Device_type> device_types;
List<Rooms> rooms;
List<Devices> devices;
Category({this.category_id,this.device_types,this.device_type_id,this.devices,this.rooms,this.room_id});
factory Category.fromJson(Map<String,dynamic>parsedJson){
var list1 = parsedJson['Device_types'] as List;
var list2 = parsedJson['Rooms'] as List;
var list3 = parsedJson['Devices'] as List;
List<Device_type> deviceTypeList =list1.map((i)=>Device_type.fromJson(i)).toList();
List<Rooms> roomsList =list2.map((i)=>Rooms.fromJson(i)).toList();
List<Devices> devicesList =list3.map((i)=>Devices.fromJson(i)).toList();
return Category(
category_id: parsedJson["category_id"],
device_type_id: parsedJson["device_type_id"],
room_id: parsedJson["room_id"],
device_types: deviceTypeList,
rooms : roomsList,
devices: devicesList
);
}
}
class Device_type {
int device_type_id, image , device_no;
String title ;
Device_type ({this.device_type_id,this.title,this.image,this.device_no});
factory Device_type.fromJson(Map<String,dynamic>parsedJson){
return Device_type(
device_type_id: parsedJson["device_type_id"],
title: parsedJson["title"],
image: parsedJson["image"],
device_no: parsedJson["device_no"],
);
}
}
class Rooms {
int id, image , device_no,roomcode;
String description,title ;
Rooms ({this.id,this.title,this.image,this.device_no,this.roomcode,this.description});
factory Rooms.fromJson(Map<String,dynamic> parsedJson){
return Rooms(
id: parsedJson["id"],
title:parsedJson["title"],
image: parsedJson["image"],
device_no: parsedJson["device_no"],
roomcode: parsedJson["roomcode"],
description: parsedJson["description"]
);
}
}
class Devices {
int device_id, image_path ,type,status,fav_flag, category_id;
String description,title ;
Devices ({this.device_id,this.title,this.image_path,
this.description,this.fav_flag,this.status,this.type,this.category_id});
factory Devices.fromJson(Map<String,dynamic> parsedJson){
return Devices(
device_id: parsedJson["device_id"],
title:parsedJson["title"],
image_path: parsedJson["image_path"],
type: parsedJson["type"],
status: parsedJson["status"],
fav_flag: parsedJson["fav_flag"],
category_id : parsedJson['category_id'],
description: parsedJson["description"]
);
}
}
当我跟踪项目以找出问题所在时,我意识到它只循环了一次,所以数据将永远为空,不知道为什么 任何帮助将不胜感激