flutter登录功能完成:主要解决future返回一个对象还是列表的报错。

文章介绍了如何在Flutter中创建一个可序列化的LoginEntity模型类,包括其构造函数、从JSON转换和序列化方法,以及一个模拟的登录请求函数。还展示了如何在页面上发送登录请求并传递必要的参数。
摘要由CSDN通过智能技术生成

1、创建一个model对象类

()
class LoginEntity {
 late int code = 0;
 late String message = '';
 late String token = '';

 LoginEntity();

 factory LoginEntity.fromJson(Map<String, dynamic> json) => $LoginEntityFromJson(json);

 Map<String, dynamic> toJson() => $LoginEntityToJson(this);

 
 String toString() {
  return jsonEncode(this);
 }
}

2、登录请求

// 登录,返回一个对象
static Future<LoginEntity> login(String phoneNumber,String phoneCode) async{
  final url = "login/";
  final res = await HttpRequest.request(url,method: "post",
      params: {"phone_number":phoneNumber,"phone_code":phoneCode});

  // 解决'_Map<String, dynamic>' is not a subtype of type 'FutureOr<LoginEntity>'的报错!!!!!!!!
  final Map<String,dynamic> jsonData = res;
  final LoginEntity loginEntity = LoginEntity.fromJson(jsonData);

  return loginEntity;
}

3、页面发请求,传递登录接口需要的参数

// 请求登录接口
Future _login(String phoneNumber,String phoneCode) async{
  final LoginEntity res = await LoginRequest.login(phoneNumber, phoneCode);
  print(res.code);
}

return GestureDetector(
  onTap: (){
    // 6、访问父组件的属性,要使用widget.属性
    print('手机号:${widget.phoneNumberController.text}');
    print('验证码:${widget.phoneCodeController.text}');
    _login(widget.phoneNumberController.text,widget.phoneCodeController.text);
  },
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值