flutter - Flutter 中的 PageView.Builder 在 null 上调用了 getter 'length'。接收方:null 尝试调用:长度
问题描述
当使用来自 API 的响应时显示错误 在 null 上调用了 getter 'length'。接收方:null 尝试调用: 这里的长度是我的 API 代码
var url =
"https://domain.php";
var res;
var splashs;
void initState() {
super.initState();
fetchData();
}
fetchData() async {
res = await http.get(url);
splashs = jsonDecode(res.body);
setState(() {});
}
同时使用 List 然后代码正常工作
List<Map<String, String>> splashs = [
{
"header": "Flatros",
"text": "Welcome to Flatros, Let’s shop!",
"image_name": "assets/images/splash_1.png"
},
{
"header": "Shopping",
"text":
"We help people conect with store \naround United State of America",
"image_name": "assets/images/splash_2.png"
},
{
"header": "Multi Category",
"text": "FInal Screen",
"image_name": "assets/images/splash_3.png"
},
];
解决方案
可能您需要将数据转换为List
或Array
List splashs = new List();
fetchData() async {
res = await http.get(url);
final data = jsonDecode(res.body);
for (var i in data) {
splashs.add(i); // Create a list and add data one by one
}
setState(() {});
}
推荐阅读
- c# - 无法在 MqttNet 托管客户端中添加客户端证书
- python - 如何在 Python、Selenium 中更改执行控制
- python - Python打开一个新的.txt文件,保存,然后将文件路径打印到控制台
- javascript - setAttribute onClick 函数 - React JS
- angularjs - 由 AngularJs 1 $http.get 填充的数据表说“表中没有可用数据”
- mysql - SQL查询按用户列表计算Woocommerce订单数
- python - 生成一个随机数(1-2)作为变量,然后让python根据生成的变量做两件事之一
- python - 如何在两列上分组并从python中的第三列获取相应组合的值?
- amazon-web-services - QuickSight 报告为 pdf 格式而不是 CSV
- html - CSS 边框错误导致我的整个网站显示有边框。我该如何解决?