Flutter:配置文件数据中的 GetX 变量初始化问题
Flutter : GetX Variable initialise problem in Profile Data
我在我的 flutter 应用程序上使用 GetX 状态管理。我想从服务器获取用户的个人资料数据。这就是我创建配置文件模型 Class 和控制器屏幕的原因。但我不能。这是初始化的问题。
json 响应 -
{
"riderId": 1,
"riderName": "Ramiz Miah",
"riderContact": "01787656565",
"riderEmail": "ramiz@hotmail.com",
"riderImgRef": ""
}
个人资料模型
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;
}
}
控制器
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;
}, onError: (err) {
debugPrint(err.toString());
});
} catch (e) {
debugPrint(e.toString());
}
}
}
和API服务
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');
}
}
}
现在我的问题是如何在此处定义此变量 (RxMap<String, dynamic> profileDetails = <String, dynamic>{}.obs;
),这就是它正常工作的原因
我解决了这个问题
Rx<ProfileModel?> profile = ProfileModel().obs;
谢谢。
我在我的 flutter 应用程序上使用 GetX 状态管理。我想从服务器获取用户的个人资料数据。这就是我创建配置文件模型 Class 和控制器屏幕的原因。但我不能。这是初始化的问题。
json 响应 -
{
"riderId": 1,
"riderName": "Ramiz Miah",
"riderContact": "01787656565",
"riderEmail": "ramiz@hotmail.com",
"riderImgRef": ""
}
个人资料模型
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;
}
}
控制器
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;
}, onError: (err) {
debugPrint(err.toString());
});
} catch (e) {
debugPrint(e.toString());
}
}
}
和API服务
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');
}
}
}
现在我的问题是如何在此处定义此变量 (RxMap<String, dynamic> profileDetails = <String, dynamic>{}.obs;
),这就是它正常工作的原因
我解决了这个问题
Rx<ProfileModel?> profile = ProfileModel().obs;
谢谢。