打印出Instance of 'RegistrationRequest'是什么,是不是打印不出来啊

来源:4-7 注册模块设计与实现

钓鱼的猫_fwEJMS

2025-02-25

图片描述

写回答

1回答

CrazyCodeBoy

2025-02-26

在你打印 RegistrationRequest 时看到输出 Instance of 'RegistrationRequest',这是因为 Dart 默认通过 toString() 方法打印对象时,会返回对象的类型和内存地址,除非你自定义了 toString() 方法。

要解决这个问题,你需要在 RegistrationRequest 类中覆盖 toString() 方法,这样当你打印这个对象时,它会返回你期望的字符串内容,而不是默认的实例信息。

例如,假设 RegistrationRequest 类是这样的:

class RegistrationRequest {
  final String userName;
  final String password;
  final String moocId;
  final String orderId;

  RegistrationRequest({
    required this.userName,
    required this.password,
    required this.moocId,
    required this.orderId,
  });

  // 覆盖toString方法
  @override
  String toString() {
    return 'RegistrationRequest(userName: $userName, password: $password, moocId: $moocId, orderId: $orderId)';
  }
}

在这里,toString() 方法返回了 RegistrationRequest 对象的详细信息,格式化成你想要的形式。这样你在打印对象时就会看到类似如下的信息:

RegistrationRequest(userName: testUser, password: testPassword, moocId: 12345, orderId: 67890)

现在,在你的 send() 方法中打印 request 时,应该可以正确输出 RegistrationRequest 对象的内容,而不是 Instance of 'RegistrationRequest'。

0
1
钓鱼的猫_fwEJMS
原来如此啊
2025-02-26
共1条回复

Flutter高级进阶实战-仿哔哩哔哩-掌握Flutter高阶技能

一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案

1746 学习 · 882 问题

查看课程