我正在实现与提供者的登录,但没有在仪表板页面上获取数据。 模型类
class LoginModel {
Data data;
int status;
String message;
LoginModel({this.data, this.status, this.message});
LoginModel.fromJson(Map<String, dynamic> json) {
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
status = json['status'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data.toJson();
}
data['status'] = this.status;
data['message'] = this.message;
return data;
}
}
class Data {
String customerId;
String customerMobileNo;
String customerToken;
String otp;
Data({this.customerId, this.customerMobileNo, this.customerToken, this.otp});
Data.fromJson(Map<String, dynamic> json) {
customerId = json['customerId'];
customerMobileNo = json['customerMobileNo'];
customerToken = json['customerToken'];
otp = json['otp'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['customerId'] = this.customerId;
data['customerMobileNo'] = this.customerMobileNo;
data['customerToken'] = this.customerToken;
data['otp'] = this.otp;
return data;
}
}
提供商类
class AuthProvider extends ChangeNotifier {
Future<LoginModel> generateOTP(String mobileNumber) async {
var result;
Response response = await post(
AppUrl.login,
body: {
'mobileNo': mobileNumber,
},
);
if(response.statusCode==200) {
final responseData = json.decode(response.body);
var userData = responseData['data'];
print(responseData);
LoginModel authUser = LoginModel.fromJson(userData);
notifyListeners();
}
else {
print("Something went wrong");
}
return result;
}
}
显示页面
class Dashboard extends StatelessWidget {
@override
Widget build(BuildContext context) {
final userTest = Provider.of<AuthProvider>(context);
return Scaffold(
body: Center(
child: ListView(
shrinkWrap: true,
children: [
Text(userTest.authUser.data.customerToken),
],
),
),
);
}
错误
在处理手势时引发了以下NoSuchMethodError: getter'customerToken'在null上被调用。 接收者:null 尝试调用:customerToken
如何访问LoginModel类的属性。有人可以解决我的问题吗,请帮帮我,我尝试过乐透,但我无法获得价值。
答案 0 :(得分:0)
LoginModel authUser = LoginModel.fromJson(userData);
您正在初始化方法变量LoginModel authUser
而不是类变量authUser
的值,请尝试从'authUser'之前删除LoginModel
。Dashboard
中,您还应该检查null
的值,您可以像这样userTest?.authUser?.data?.customerToken ?? ''
答案 1 :(得分:0)
generateOTP()
函数放在changenotifier类中,该类将帮助您在需要时通知您的侦听器,并将其范围限制在您的小部件中。