由于我正在创建一个应用程序,其中所有COVID数据将通过API根据其区域进行排列,而API位于MAP<字符串,字符串>形式。但我可能是打字错误了。API URL=https://api.rootnet.in/covid19-in/stats/latest
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
你能帮我解决这个问题吗?作为一个新手,这就是为什么我会犯这些错误。请帮我克服这个问题。谢谢:}
我刚刚解决了这个
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();
}
删除列表的类型并使其成为动态列表。这肯定会有所帮助。简单