我正在尝试将数据加载到日历中,但无法这样做,因为从服务器检索数据时出现以下错误:
[错误:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常:闭包:(动态)=>空
我的目标是成功将数据加载到_carList []列表中,然后在日历上查看它,但是一旦将数据传输到列表中,就会突然抛出上述错误。因此,请您帮忙我在解决这个问题。
下面是必要的代码:
CarProvider.dart
List<Cars> _carList = [];
Future<void> fetchandSetProduct() async {
Map<String, String> headers = {
"Content-type": "application/json"};
const url = 'http://localhost:3000/users/cars/1111';
try{
final response = await http.get(url, headers: headers);
List<Cars> cardata = List<Cars>();
var data = jsonDecode(response.body);
// print(data);
data.forEach((value) {
print(value);
cardata.add(Cars.fromJson(value));
}
);
// print(cardata);
_carList = cardata;
print(_carList);
notifyListeners();
}catch(error){
print(error);
}throw(error) {
print(error);
};
}
}
错误日志
E/flutter (14760): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Closure: (dynamic) => Null
E/flutter (14760): #0 CarProvider.fetchandSetProduct
package:education/providers/CarProvider.dart:93
E/flutter (14760): <asynchronous suspension>
E/flutter (14760): #1 _CalendarSelectState.didChangeDependencies
calendar.dart
var _init = true;
@override
void didChangeDependencies() {
if(_init) {
Provider.of<CarProvider>(context,listen: false).fetchandSetProduct();
}
_init = false;
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
// calendar