this below json
code is our server response which i parse that with this code:
@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();
here i don't have any problem and i can get all of values with onlineBakers
variable, but i can't resolve analysis error in AndroidStudio
as this error for this part of code:
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>),
),
when i try to use as List<UserModel>
i get another error, for example:
success['data'].map(
(data) => UserModel.fromJson(data as Map<String, dynamic>),
) as List<UserModel>,
error:
type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'List<UserModel>' in type cast
response 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"
}
}
}
]
UPDATED
my repository to get data from server:
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;
}
}
CodePudding user response:
It's not as elegant as all in a row, but the type of each variable will be stated.
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);
}
}
}
CodePudding user response:
The result of success['data'].map
is not list, try this:
success['data'].map(
(data) => UserModel.fromJson(data as Map<String, dynamic>),
).toList(),