json - 将http中的json数据解码为列表,但列表在颤动中为空
问题描述
我正在尝试将 json 数据从 http 解码到一个列表中,但是在我设置数据后列表为空。我还有其他方法可以做到吗?我正在使用有状态的小部件并在 initstate 中调用该函数。http 请求获取 json 数据,但我使用 ListView.builder 的构建函数没有执行。脚手架是空的。不打印 ListView 内的打印语句。我什至在列表视图中尝试了纯文本,即使它没有显示屏幕。
List body;
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class SellerSign extends StatefulWidget {
@override
_SellerSignState createState() => _SellerSignState();
}
class _SellerSignState extends State<SellerSign> {
List body;
@override
void initState(){
this.shopment();
}
shopment() async {
var headers = {
'API-Key': 'TEST_f+y9/tHG+yVxEq3uS3H1ogfezHSCWSq5MsIXUOnIV+Q',
'Content-Type': 'application/json',
};
var data = '{ "rate_options": { "carrier_ids": [ "se-634124" ] }, "shipment": { "validate_address": "no_validation", "ship_to": { "name": "Amanda Miller", "phone": "555-555-5555", "address_line1": "525 S Winchester Blvd", "city_locality": "San Jose", "state_province": "CA", "postal_code": "95128", "country_code": "US", "address_residential_indicator": "yes" }, "ship_from": { "company_name": "Example Corp.", "name": "John Doe", "phone": "111-111-1111", "address_line1": "4009 Marathon Blvd", "address_line2": "Suite 300", "city_locality": "Austin", "state_province": "TX", "postal_code": "78756", "country_code": "US", "address_residential_indicator": "no" }, "packages": [ { "weight": { "value": 1.0, "unit": "ounce" } } ] } }';
var res = await http.post('https://api.shipengine.com/v1/rates', headers: headers, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
setState(() {
body = json.decode(res.body);
});
}
@override
Widget build(BuildContext context){
return new Scaffold(
appBar: new AppBar(title: new Text("Listviews"), backgroundColor: Colors.blue),
body: new ListView.builder(
itemCount: body == null ? 0 : body.length,
itemBuilder: (BuildContext context, int index){
print("kbhkjb${body[index]["rate_id"]}");
return new Card(
child: new Text(body[index]["rate_id"]),
);
},
),
);
}
}
这是打印语句。
I/flutter (26156): {
I/flutter (26156): "rate_response": {
I/flutter (26156): "rates": [
I/flutter (26156): {
I/flutter (26156): "rate_id": "se-720961365",
I/flutter (26156): "rate_type": "shipment",
I/flutter (26156): "carrier_id": "se-634124",
I/flutter (26156): "shipping_amount": {
I/flutter (26156): "currency": "usd",
I/flutter (26156): "amount": 0.5100
I/flutter (26156): },
I/flutter (26156): "insurance_amount": {
I/flutter (26156): "currency": "usd",
I/flutter (26156): "amount": 0.0
I/flutter (26156): },
I/flutter (26156): "confirmation_amount": {
I/flutter (26156): "currency": "usd",
I/flutter (26156): "amount": 0.0
I/flutter (26156): },
I/flutter (26156): "other_amount": {
I/flutter (26156): "currency": "usd",
I/flutter (26156): "amount": 0.0
I/flutter (26156): },
I/flutter (26156): "zone": 7,
I/flutter (26156): "package_type": "letter",
I/flutter (26156): "delivery_days": 3,
I/flutter (26156): "guaranteed_service": false,
I/flutter (26156): "estimated_delivery_date": "2021-06-28T00:00:00Z",
I/flutter (26156): "carrier_delivery_days": "3",
I/flutter (26156): "ship_date": "2021-06-24T00:00:00Z",
I/flutter (26156): "negotiated_rate": false,
I/flutter (26156): "service_type": "USPS First Class Mail",
I/flutter (26156): "service_code": "usps_first_class_mail",
I/flutter (26156): "trackable": false,
I/flutter (26156): "carrier_code": "stamps_com",
I/flutter (26156): ```
解决方案
jsonDecode
null
如果接收到的 JSON 为 null(即 JSON 字符串只是),将返回'null'
:
final json = jsonDecode('null');
expect(json, null); // passes
确保 API 没有返回null
json(尽管根据我的经验,这并不常见)。
另一个可能的错误来源是如果您在设置之前尝试使用body
它。
当你创建时List body;
,假设你没有在空安全模式下运行,它被初始化为null
;
如果您有一个build()
尝试body
立即使用的方法,您将尝试在网络调用完成之前构建 UI。如果是这种情况,您可以检查 if body
is null
,并在它是时显示加载微调器。
推荐阅读
- mysql - 如何在 MySQL 中交叉两个查询
- git - 启用 GPG 签名的交互式 rebase 失败
- c - 了解洗牌功能
- regex - 有没有更好的方法来编写正则表达式来匹配案例,
- spring-boot - 如何让 AWS Lambda 和 Spring Boot 2.1.5 使用 Log4j2.xml 并登录到 Cloudwatch?
- nginx - 在基于 Docker 的 ElasticBeanstalk 上运行的 nginx 上处理子域
- html - 拆分文件文本 Angular
- javascript - 是否可以在数据属性中使用 json 内容应用 css 规则?
- google-cloud-dataflow - 是否有用于 Google Dataflow 的 Java 客户端从模板开始工作?
- python-3.x - 仅基于一个数字在python 3中创建一个二维数组/列表以创建一个空白矩阵