You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart中无法访问对象属性问题求助

Dart对象属性无法访问问题解决

问题现象

  • 开发中无法访问对象属性,调试打印tempo.id无输出
  • 直接调用其他文件的对象实例时,打印对象仅显示Instance of Object$,无法看到属性值
  • 当前使用GetIt测试数据库功能,本地数据存储在项目其他文件中

相关代码

1. 对象实例创建代码

studentProfilecreation tempo = studentProfilecreation("Firebaseid1", "A", "B", '', "tdd");

2. Widget文件调用代码

Widget build(BuildContext context) {
    studentProfilecreation temp = tempo;
    String id = tempo.id;
    print(id);
    MediaQueryData queryData;
}

3. studentProfilecreation类定义

class studentProfilecreation {
  String id = "";
  String firstName = "";
  String lastName = "";
  String profileImage = '';
  String major = "";
  String bio = "";
  Map<String, File> coursesAndnotes = Map();
//Constructor
  studentProfilecreation(
    String id,
    String firstName,
    String lastName,
    String profileImage,
    String major,
  ) {
    id = id;
    firstName = firstName;
    lastName = lastName;
    profileImage = profileImage;
    major = major;
  }

  studentProfilecreation.studentProfilecreationempty() {
    id = "";
    firstName = "";
    lastName = "";
    profileImage = '';
    major = "";
  }

  studentProfilecreation.createcopy(studentProfilecreation original) {
    id = original.id;
    firstName = original.firstName;
    lastName = original.lastName;
    profileImage = original.profileImage;
    major = original.major;
  }
}

4. 目标Widget代码

class studentProfile extends StatelessWidget {
  String id = "";
  studentProfile(String id) {
    this.id = id;
  }

  @override
  Widget build(BuildContext context) {
    studentProfilecreation temp = tempo;
    String id = tempo.id;
    print(id);
}

问题根源

核心问题出在studentProfilecreation类的构造函数中:构造函数里的赋值语句(如id = id)只是给参数变量赋值,并没有修改类的成员变量。因为参数名和成员变量名相同,没有用this.区分,导致赋值操作完全无效,类的成员变量始终保持初始的空字符串值。

另外,打印对象显示Instance of Object$是因为类没有重写toString()方法,默认的toString只能输出实例类型,无法展示属性内容。

修复方案

1. 修正构造函数赋值

三种可选方式:

  • 方式一:用this.明确指向成员变量
studentProfilecreation(
    String id,
    String firstName,
    String lastName,
    String profileImage,
    String major,
  ) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.profileImage = profileImage;
    this.major = major;
  }
  • 方式二:使用初始化列表(推荐写法)
studentProfilecreation(
    String id,
    String firstName,
    String lastName,
    String profileImage,
    String major,
  ) : id = id,
       firstName = firstName,
       lastName = lastName,
       profileImage = profileImage,
       major = major;
  • 方式三:使用构造函数语法糖(最简洁)
studentProfilecreation(
    this.id,
    this.firstName,
    this.lastName,
    this.profileImage,
    this.major,
  );

2. 规范命名(可选但推荐)

遵循Dart命名规范:类名用大驼峰,构造函数别名用小驼峰:

class StudentProfileCreation {
  // ...成员变量...

  StudentProfileCreation(this.id, this.firstName, this.lastName, this.profileImage, this.major);

  StudentProfileCreation.empty() {
    id = "";
    firstName = "";
    lastName = "";
    profileImage = '';
    major = "";
  }

  StudentProfileCreation.copy(StudentProfileCreation original) {
    id = original.id;
    firstName = original.firstName;
    lastName = original.lastName;
    profileImage = original.profileImage;
    major = original.major;
  }
}

3. 重写toString()方便调试

添加后打印对象可直接查看属性值:

@override
String toString() {
  return 'StudentProfileCreation{id: $id, firstName: $firstName, lastName: $lastName, profileImage: $profileImage, major: $major}';
}

内容的提问来源于stack exchange,提问作者Edgar Torres

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 23:27:03