I am using GetX statemangement on my flutter app. I want to fetch User's Profile data from server. That's Why I create a Profile Model Class and a Controller Screen. But I can't. Here is a problem to initialisation.
json Response -
{
"riderId": 1,
"riderName": "Ramiz Miah",
"riderContact": "01787656565",
"riderEmail": "[email protected]",
"riderImgRef": ""
}
Profile Model
class ProfileModel {
int? riderId;
String? riderName;
String? riderContact;
String? riderEmail;
String? riderImgRef;
ProfileModel(
{this.riderId,
this.riderName,
this.riderContact,
this.riderEmail,
this.riderImgRef});
ProfileModel.fromJson(Map<String, dynamic> json) {
riderId = json['riderId'];
riderName = json['riderName'];
riderContact = json['riderContact'];
riderEmail = json['riderEmail'];
riderImgRef = json['riderImgRef'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['riderId'] = riderId;
data['riderName'] = riderName;
data['riderContact'] = riderContact;
data['riderEmail'] = riderEmail;
data['riderImgRef'] = riderImgRef;
return data;
}
}
Controller
class ParofileController extends GetxController {
RxMap<String, dynamic> profileDetails = <String, dynamic>{}.obs;
//<============= Fetch and Assign DashBoard Today Details List
void fetchandAssignProfileDetails() async {
try {
ProfileApiService().getProfileDetails().then((resp) {
profileDetails.value = resp;
}, one rror: (err) {
debugPrint(err.toString());
});
} catch (e) {
debugPrint(e.toString());
}
}
}
And API Service
class ProfileApiService extends GetConnect {
Future<ProfileModel?> getProfileDetails() async {
Uri url = Uri.parse("${AppConfig.baseUrl}/Rider/GetRiderDetails");
var response = await http.get(
url,
);
if (response.statusCode == 200) {
return ProfileModel.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load User Profile');
}
}
}
And Now my Question is How to Define this variable here ( RxMap<String, dynamic> profileDetails = <String, dynamic>{}.obs;
), that's why it's work properly
CodePudding user response:
I solved this
Rx<ProfileModel?> profile = ProfileModel().obs;
thanks.