在颤动中加载 api 时出现类型转换错误
Posted
技术标签:
【中文标题】在颤动中加载 api 时出现类型转换错误【英文标题】:Getting typecasting error while api loading in flutter 【发布时间】:2020-12-29 07:24:21 【问题描述】:当我创建一个应用程序时,所有 COVID 数据都将通过 API 根据其区域进行排列,并且 API 采用 MAP
List<String> redZoneData;
List<String> orangeZoneData;
List<String> greenZoneData;
int n = 2;
Map mapData;
var totalConfirmed;
var dataCalc;
var death;
var stateCode;
mapDataValue() async
final url = 'https://api.rootnet.in/covid19-in/stats/latest';
final response = await http.get(url);
mapData = json.decode(response.body);
if (response.statusCode == 200)
setState(()
for (int index = 0;
index < mapData['data']['regional'].length;
index++)
totalConfirmed = mapData['data']['regional'][index]['totalConfirmed'];
death = mapData['data']['regional'][index]['deaths'];
dataCalc = double.parse((totalConfirmed / death).toStringAsFixed(2));
stateCode = mapData['data']['regional'][index]['loc'];
if (dataCalc <= 40.00)
redZoneData.add(stateCode);
else
if (dataCalc > 40.00 && dataCalc <= 50.00)
orangeZoneData.add(stateCode);
else
greenZoneData.add(stateCode);
// print(redZoneData);
// print(orangeZoneData);
// print(greenZoneData);
);
else
throw Exception('loading failed...');
@override
void initState()
mapDataValue();
super.initState();
错误:
E/flutter (17875): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'add' was called on null.
E/flutter (17875): Receiver: null
E/flutter (17875): Tried calling: add("Andaman and Nicobar Islands")
E/flutter (17875): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter (17875): #1 _MapIntegrationState.mapDataValue.<anonymous closure>
package:CovidTracker/MapIntegration.dart:42
E/flutter (17875): #2 State.setState
package:flutter/…/widgets/framework.dart:1240
E/flutter (17875): #3 _MapIntegrationState.mapDataValue
package:CovidTracker/MapIntegration.dart:28
E/flutter (17875): <asynchronous suspension>
E/flutter (17875): #4 _MapIntegrationState.initState
package:CovidTracker/MapIntegration.dart:57
E/flutter (17875): #5 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4684
E/flutter (17875): #6 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
E/flutter (17875): #7 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (17875): #8 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5991
E/flutter (17875): #9 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (17875): #10 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (17875): #11 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5874
E/flutter (17875): #12 Element.inflateWidget
你能帮我解决这个问题吗?因为我是新手,所以我收到这些错误。请帮助我克服这一点。 谢谢:
【问题讨论】:
在你做.add()
之前检查stateCode
的空能力。另外,检查add()
函数是否正常工作,无论它在哪里。
一分钟前我解决了它。顺便谢谢你的回复:呵呵。对不起,我昨天上传了问题,但没有人回答这就是我向你寻求帮助的原因:)
伟大的 Shivam,这就是你学习的方式。你走在正确的道路上。再说一次,不要担心否决票。不断改进。一切顺利:)
还有一个建议Shivam,如果你已经找到解决方案,那么也回答你的问题。人们会发现它在未来很有用/有帮助,无论谁会像你一样面临同样的问题。来吧,回答它:)
@Alok 感谢您的指导。我是新来的,这就是为什么不知道要做的事情。但是像你这样的人支持新手:) 再次感谢:
【参考方案1】:
我刚刚解决了这个问题
List redZoneData = new List();
List orangeZoneData = new List();
List greenZoneData = new List();
int n = 2;
Map mapData;
var totalConfirmed;
var dataCalc;
var death;
var stateCode;
mapDataValue() async
final url = 'https://api.rootnet.in/covid19-in/stats/latest';
final response = await http.get(url);
mapData = json.decode(response.body);
if (response.statusCode == 200)
setState(()
for (int index = 0;
index < mapData['data']['regional'].length;
index++)
totalConfirmed = mapData['data']['regional'][index]['totalConfirmed'];
death = mapData['data']['regional'][index]['deaths'];
dataCalc = double.parse((totalConfirmed / death).toStringAsFixed(2));
stateCode = mapData['data']['regional'][index]['loc'];
if (dataCalc <= 40.00)
redZoneData.add(stateCode);
else
if (dataCalc > 40.00 && dataCalc <= 50.00)
orangeZoneData.add(stateCode);
else
greenZoneData.add(stateCode);
// print(redZoneData);
// print(orangeZoneData);
// print(greenZoneData);
);
else
throw Exception('loading failed...');
@override
void initState()
mapDataValue();
super.initState();
删除列表的类型并使其动态化。这肯定会有所帮助。 简单
【讨论】:
为你的努力+1 :) 另外,从下一次开始你可以更精确,即只写真正解决你问题的东西,你会美好的。因为他们已经可以看到您的问题并弄清楚了。以上是关于在颤动中加载 api 时出现类型转换错误的主要内容,如果未能解决你的问题,请参考以下文章
在 Windows 窗体设计器中加载窗体时出现“找不到类型”错误
“尝试在浏览器窗口中加载我的flask应用程序时出现URL未找到错误”
Swift 解析 JSON 时出现问题:无法将“__NSCFDictionary”类型的值转换为“NSArray”错误