我在颤振中学习 api 和 http 请求,我在发出 get 请求时出错我收到了这个错误:错误:期望一个“字符串”类型的值,但得到一个“空”类型的值
我收到了这个错误:
例如,当您将 data[id] 说成 String 时,这意味着:data[id] 为空。
factory Formule.fromMap(Map? data) {
if (data == null) {
return const Formule(
id: '', title: '', prix: 0, nombreDePlace: 0, alertThreshold: 0, remainingPlaces: 0);
}
return Formule(
id: data['id'] as String,
title: data['title'] as String,
prix: data['prix'] as double,
nombreDePlace: data['nombreDePlace'] as int,
alertThreshold: data['alertThreshold'] as int,
remainingPlaces: data['remainingPlaces'] as int);
}
2个解决方案:
id: data['id'] as String?,
或者
id: data['id'] as String? ?? "",