如何使用提供程序获取数据?

问题描述:

我正在实现与提供者的登录,但没有在仪表板页面上获取数据.模型类

I am implementing login with provider but I am not getting data on dashboard page. Model Class

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

The following NoSuchMethodError was thrown while handling a gesture: The getter 'customerToken' was called on null. Receiver: null Tried calling: customerToken

如何访问LoginModel类的属性.谁能解决我的问题,请帮帮我,我试过了乐透,但我没有价值.

How can I access the property of LoginModel class. Can anyone solve my query please help me, I tried a lott but I can not get value.

  1. LoginModel authUser = LoginModel.fromJson(userData); 您正在初始化方法变量 LoginModel authUser 的值,而不是类变量 authUser 尝试删除 LoginModel 在"authUser"之前.
  2. Dashboard 中,您还应该检查 null 值,您可以像这样 userTest?.authUser?.data?.customerToken那样进行操作?''
  1. LoginModel authUser = LoginModel.fromJson(userData); you are initializing value to method variable LoginModel authUser not a class variable authUser try removing LoginModel from before 'authUser'.
  2. In Dashboard you should also check for null value, you can do that like this userTest?.authUser?.data?.customerToken ?? ''