Last active
March 19, 2021 06:40
-
-
Save blisssan/0e37dbf0fe64e0fe8eb7206808dae3cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonSerializable(explicitToJson: true) | |
class Outlet { | |
int id; | |
String name; | |
String code; | |
String address; | |
@JsonKey(name: 'mobile_logo') | |
String logo; | |
@JsonKey(name: 'company_id') | |
int companyId; | |
@JsonKey(name: 'food_items') | |
List<Map<String,dynamic>> foodItems; | |
@JsonKey(name: 'food_ids') | |
List foodIds; | |
Outlet(); | |
factory Outlet.fromJson(Map<String, dynamic> json) => | |
_$OutletFromJson(json); | |
Map<String, dynamic> toJson() => _$OutletToJson(this); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this class has only 8 properties | |
// two of which are int & 2 are list | |
class Outlet { | |
int id; | |
String name; | |
String code; | |
String address; | |
String logo; | |
int companyId; | |
List<Map<String, dynamic>> foodItems; | |
List<int> foodIds; | |
Outlet(); | |
Outlet fromJson(Map<String, dynamic> json) { | |
var outlet = Outlet(); | |
if(json['id'] != null && json['id'] == 'String'){ | |
outlet.id = int.tryParse(json['id']) ?? json['id']; | |
} | |
if(json['company_id'] != null && json['company_id'] == 'String'){ | |
outlet.companyId = int.tryParse(json['company_id']) ?? json['company_id']; | |
} | |
if(json['food_items'] != null && json['food_items'] is String){ | |
List _foodItems = jsonDecode(json['food_items']); | |
outlet.foodItems = List<Map<String,dynamic>>.from(_foodItems.map((e) => jsonDecode(e)).toList()); | |
// | |
// If this food item contains same kind of properties this this | |
// entire structure will be looped followed within that too to parse correct | |
// data type depending on how many nesting levels required | |
// | |
} | |
// And in some places we have list encoded as string, so we have to find those places and add another | |
// level of jsonDecode | |
if(json['food_ids'] != null && json['food_ids'] is String){ | |
List<String> _foodIds = jsonDecode(json['food_ids']); | |
foodIds = List<int>.from(_foodIds.map((e) => int.tryParse(e) ?? 0).toList()); | |
} | |
name = json['name']; | |
code = json['code']; | |
address = json['address']; | |
logo = json['mobile_logo']; | |
return outlet; | |
} | |
Map<String, dynamic> toJson(Outlet instance) { | |
Map<String, dynamic> _temp = {}; | |
_temp['id'] = id; | |
_temp['name'] = name; | |
_temp['code'] = code; | |
_temp['address'] = address; | |
_temp['mobile_logo'] = logo; | |
_temp['company_id'] = companyId; | |
_temp['food_ids'] = jsonEncode(foodIds); | |
var _foodItems = foodItems; | |
_foodItems.map((e) => jsonEncode(e)); | |
// repeat the nested values in each model until it covers all sub levels | |
_temp['food_items'] = jsonEncode(_foodItems); | |
return _temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment