我正在从json-generator.com检索json数据,并将其解析以在以下代码中创建公司列表。当我打印JsonData变量时,我可以看到数据,但是snapshot.data为null。这是我的代码
import 'package:flutter/material.dart';
import './Object.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'UI_Styles/Company_Card_Style.dart';
import 'dart:convert';
class Companies extends StatefulWidget {
@override
_CompaniesState createState() => _CompaniesState();
}
class _CompaniesState extends State<Companies> {
Future<List<Company>> _getCompanies() async {
var data = await http.get("http://www.json-generator.com/api/json/get/bUYmnsimgi?indent=2");
var jsonData = json.decode(data.body);
List<Company> companies = [];
for (var c in jsonData) {
Company company = Company(
c["name"],
c["opportunites"],
c["address"],
c["city"],
c["state"],
c["country"],
c["zipcode"],
c["phone"],
c["timezone"],
c["pipelineRevenue"],
c["revenueAchieved"],
c["tags"]);
companies.add(company);
}
return companies;
}
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder(
future: _getCompanies(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return CompanyCardStyle(company: snapshot.data[index]);
});
} else {
return Center(
child: CircularProgressIndicator(),
);
}
}));
}
}
这是我公司的对象
class Company {
String name;
String address;
int opportunities;
int pipelineRevenue;
int revenueAchieved;
String city;
String state;
String country;
int zipcode;
double phone;
String timezone;
String tags;
Company(
this.name,
this.address,
this.opportunities,
this.pipelineRevenue,
this.revenueAchieved,
this.city,
this.state,
this.country,
this.zipcode,
this.phone,
this.timezone,
this.tags);
}
我认为for循环存在一些问题,但我无法弄清楚什么是错误的,我是新来解析此类数据的人。
您做错了一些事情:
来自端点的某些字段输入了错误的信息data type
(电话号码为,String
但您使用了double
。tags
也返回了,List
但您使用了String
。
在将转换jsonData
为Company
对象时,您将大多数构造函数字段相互交换了。
解:
Company
类创建工厂构造函数,以简化json反序列化。您可以查看官方文档中的示例。注意:我将tag
字段设置为a,String
并且仅访问api中列表中的第一个元素。
公司类别
class Company {
String name;
String address;
int opportunities;
int pipelineRevenue;
int revenueAchieved;
String city;
String state;
String country;
int zipcode;
String phone;
String timezone;
String tags;
Company(
this.name,
this.address,
this.opportunities,
this.pipelineRevenue,
this.revenueAchieved,
this.city,
this.state,
this.country,
this.zipcode,
this.phone,
this.timezone,
this.tags);
// make a constructor for converting json to a company object
factory Company.fromJson(Map<String, dynamic> json) {
return Company(
json["name"],
json["address"],
json["opportunities"],
json["pipelineRevenue"],
json["revenueAchieved"],
json["city"],
json["state"],
json["country"],
json["zipcode"],
json["phone"],
json["timezone"],
json["tags"][0],
);
}
}
class Companies extends StatefulWidget {
@override
_CompaniesState createState() => _CompaniesState();
}
class _CompaniesState extends State<Companies> {
Future<List<Company>> _getCompanies() async {
var data = await http
.get("http://www.json-generator.com/api/json/get/bUYmnsimgi?indent=2");
var jsonData = json.decode(data.body) as List;
List<Company> companies = [];
// convert the json to a list of companies
companies = jsonData
.map((company) => Company.fromJson(company))
.toList(); // new line
print(companies.length);
return companies;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: FutureBuilder(
future: _getCompanies(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
// use your company card here
return Text(
'Company $index name: ${snapshot.data[index].name}');
});
} else {
return Center(
child: CircularProgressIndicator(),
);
}
})),
);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句