无法将飞镖变为"动态"分配给参数类型"可迭代<dynamic>"分析错误



下面的json代码是我们的服务器响应,我用下面的代码解析它:

@freezed
abstract class UserModel with _$UserModel {
const factory UserModel({
required int id,
@JsonKey(name: 'name_family') required String nameFamily,
@JsonKey(name: 'mobile_number') required String mobileNumber,
@JsonKey(name: 'baker_profile') required BakerProfileModel bakerProfile,
}) = _UserModel;
factory UserModel.fromJson(Map<String, dynamic> json) => _$UserModelFromJson(json);
}
final onlineBakers = List<UserModel>.from(
success['data'].map(
(data) => UserModel.fromJson(data as Map<String, dynamic>),
),
).toList();

这里我没有任何问题,我可以用onlineBakers变量获得所有值,但我无法解决AndroidStudio中的分析错误,因为这部分代码的错误是:

The argument type 'dynamic' can't be assigned to the parameter type 'Iterable<dynamic>'
success['data'].map(
(data) => UserModel.fromJson(data as Map<String, dynamic>),
),

当我尝试使用as List<UserModel>时,我会得到另一个错误,例如:

success['data'].map(
(data) => UserModel.fromJson(data as Map<String, dynamic>),
) as List<UserModel>,

错误:

type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'List<UserModel>' in type cast

响应json:

[
{
"id": 6,
"user_type": "baker",
"name_family": "test",
"mobile_number": "09087549785",
"active": 1,
"baker_profile": {
"id": 9,
"user_id": 6,
"province_id": 19,
"city_id": 729,
"neighbourhood_id": 2,
"profile_image": "baker_profiles_images/pUZtdPNCKoQnXbKWBwUnciK7Yo01fG1IMUggzgVK.png",
"store_image": "baker_stores_images/pUZtdPNCKoQnXbKWBwUnciK7Yo01fG1IMUggzgVK.png",
"store_name": "test",
"status": 1,
"peyk_price": 0,
"minimum_order": 1,
"maximum_order": 1,
"maximum_queue_count": 1,
"morning_start_time": 1,
"morning_end_time": 1,
"afternoon_start_time": 1,
"afternoon_end_time": 1,
"active": 1,
"bread": [
{
"id": 1,
"bread_name":"test",
"image": "breads/AJq3PHo6Ja4AkcuSsJgyvUoUi4XGOTsmplgqhgEp.png",
"price": 10000,
"gram": "250",
"bread_type": "test"
},
{
"id": 2,
"bread_name": "test",
"image": "breads/AJq3PHo6Ja4AkcuSsJgyvUoUi4XGOTsmplgqhgEp.png",
"price": 10000,
"gram": "200",
"bread_type": "test"
}
],
"province": {
"id": 19,
"name": "test"
},
"city": {
"id": 729,
"province_id": 19,
"name": "test"
},
"neighbourhood": {
"id": 2,
"city_id": 729,
"name": "test"
}
}
}
]

更新

我的存储库从服务器获取数据:

Future<Map<String, dynamic>?> getResponse(
HTTP method, String endPoint, Map<String, dynamic>? parameters) async {
try {
const r = RetryOptions(maxAttempts: 3);
final response = await r.retry(
() => _submit(method, endPoint, parameters),
retryIf: (e) => e is SocketException || e is TimeoutException,
);
return {'statusCode': response.statusCode, 'data': response.data};
} on DioError catch (e) {
throw (e.response != null
? e.response!.statusCode
: e.error.osError.errorCode) as Object;
}
}

success['data'].map的结果不在列表中,请尝试以下操作:

final onlineBakers = success['data'].map((data) =>
UserModel.fromJson(data as Map<String, dynamic>),).toList();

它不像一行中的所有变量那样优雅,但每个变量的类型都会被说明。

var onlineBakers = <UserModel>[];
var data = success['data'];
if(data is List){
for(var item in data){
if(item is Map){
onlineBakers.add(UserModel.fromJson(item);
}
}
}

相关内容

  • 没有找到相关文章

最新更新