I have a problem to load a list from a class that the class need data from several database
The database:
A. userdata (_id, name, phone)
B. mariage status (_id, userID, mariageStatus)
--> "_id" in userdata & "userID" in marriage status are thing to be matched
read the userdata:
class User {
final String idUser,
name,
phone;
User(
{this.idUser,
this.name,
this.phone});
factory User.fromJson(Map<String, dynamic> json) {
return User(
idUser: json['_id'],
name: json['name'],
phone: json['phone']);
}
}
List<User> userFromJson(jsonData) {
List<User> result =
List<User>.from(jsonData.map((item) => User.fromJson(item)));
return result;
}
// index
Future<List<User>> fetchUser() async {
String route = AppConfig.API_ENDPOINT + "userdata";
final response = await http.get(route);
if (response.statusCode == 200) {
var jsonResp = json.decode(response.body);
return userFromJson(jsonResp);
} else {
throw Exception('Failed load $route, status : ${response.statusCode}');
}
}
while to read the marriage:
class Marriage{
final String idUser, mariageStatus;
Marriage(
{this.idUser,
this.mariageStatus});
factory Marriage.fromJson(Map<String, dynamic> json) {
return Marriage(
idUser: json['userID'],
name: json['mariageStatus']);
}
}
List<Marriage> marriageFromJson(jsonData) {
List<Marriage> result =
List<Marriage>.from(jsonData.map((item) => Marriage.fromJson(item)));
return result;
}
// index
Future<List<Marriage>> fetchMarriage() async {
String route = AppConfig.API_ENDPOINT + "marriage";
final response = await http.get(route);
if (response.statusCode == 200) {
var jsonResp = json.decode(response.body);
return marriageFromJson(jsonResp);
} else {
throw Exception('Failed load $route, status : ${response.statusCode}');
}
}
then how to make a list off combination class like this?
class User_Mariage {
final String idUser,
name,
phone,
mariageStatus;
User(
{this.idUser,
this.name,
this.phone,
this.mariageStatus});
factory User.fromJson(Map<String, dynamic> json) {
return User(
idUser:
name:
phone:
mariageStatus:
}
}
List<User> userFromJson(jsonData) {
List<User> result =
List<User>.from(jsonData.map((item) => User.fromJson(item)));
return result;
}
of if there are another better way to make the list, please let me know, thank you very much
for example like User class and Marriage class?
– Alfan Dec 02 '21 at 20:48