I have a two class ShopModel and ShopInRelationshipModel and I received a nested response from supabase. Kindly help with how to convert that response to ShopInRelationshipModel object.
class ShopModel {
final int? id;
final int? adrnr;
final int? industry;
final String shop_name;
final int? currency;
const ShopModel({
this.id,
this.adrnr,
required this.industry,
required this.shop_name,
required this.currency,
});
factory ShopModel.fromJson(Map<String, dynamic> json) {
return ShopModel(
id: json['id'] ?? '',
adrnr: json['adrnr'] ?? null,
industry: json['industry'] ?? 1,
shop_name: json['shop_name'] ?? '',
currency: json['currency_id'] ?? '',
);
}
class ShopInRelationshipModel {
final int? id;
final int? shop_id;
final String? uid;
final int relationship_type;
final ShopModel? shop;
const ShopInRelationshipModel({
this.id,
required this.shop_id,
required this.uid,
required this.relationship_type,
required this.shop,
});
factory ShopInRelationshipModel.fromJson(Map<String, dynamic> json) {
return ShopInRelationshipModel(
uid: json['uid'] ?? '',
shop_id: (json['shop_id']) ?? '',
id: json['id'] ?? '',
relationship_type: json['relationship_type'] ?? 1,
shop: ShopModel.fromJson(json['shop']),
);
}
}
List<ShopInRelationshipModel> shopInRelationshipList;
final response = await _supabase.client
.from('shop_relationship')
.select('*, shop(*)')
.eq('uid', userId);
//convert response to list of ShopInRelationshipModel. Here the error is coming
shopInRelationshipList =
response.map((e) => ShopInRelationshipModel.fromJson(e)).toList();
Here is the sample response incase you need it.
response = [{id: 1, created_at: 2023-05-08T16:15:58.453985+00:00, shop_id: 11, uid: ac8ae335-2b10-47f3-8ad6-6d4c838439bf, relationship_type: 1, shop: {id: 11, created_at: 2023-05-08T16:15:34.876639+00:00, adrnr: null, industry: 1, shop_name: New Store, currency_id: 1}}, {id: 2, created_at: 2023-05-08T16:39:56+00:00, shop_id: 9, uid: ac8ae335-2b10-47f3-8ad6-6d4c838439bf, relationship_type: 1, shop: {id: 9, created_at: 2023-05-08T16:09:38.251026+00:00, adrnr: null, industry: 1, shop_name: ABC Store, currency_id: 1}}]
I am trying to convert the response from Supabase join query to ShopInRelationshipModel
but I am receiving an error.
'List<dynamic>' is not a subtype of type 'List<ShopInRelationshipModel>
Kindly help with how to convert response to List<ShopInRelationshipModel>
object.
2
Answers
add
.cast<Model>()
to the list.The code would look like this.