flutter - Dart 私有(private)字段和 vscode 调试器

标签 flutter class dart

这是一个带有私有(private)字段和构造函数的类:

class User {
  final String _id;
  final String _username;
  final String _photoUrl;
  final String _bio;
  final String _city;
  final Map<String, dynamic> _favorite;

  const User({@required String id, 
              @required String username, 
              String photoUrl, 
              String bio, 
              String city, 
              Map<String, dynamic> favorite
            })
        : this._id = id, 
          this._username = username, 
          this._photoUrl = photoUrl, 
          this._bio = bio, 
          this._city = city, 
          this._favorite = favorite ?? const{};

 User copyWith({String id, String username, String photoUrl, String bio, String city, Map<String, dynamic> favorite}){
    return User(
      id: id ?? this._id,
      username: username ?? this._username,
      photoUrl: photoUrl ?? this._photoUrl,
      bio: bio ?? this._bio,
      city: city ?? this._city,
      favorite: favorite ?? this._favorite,
    );
  }

  String get id => _id;
  String get username => _username;
  String get photoUrl => _photoUrl;
  String get bio => _bio;
  String get city => _city;
  Map<String, dynamic> get favorite => _favorite;
  
  @override 
  int get hashCode => _id.hashCode ^ _username.hashCode ^ _photoUrl.hashCode ^ _city.hashCode ^ _bio.hashCode ^ _favorite.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is User &&
          runtimeType == other.runtimeType &&
          _id == other._id;
  @override 
  String toString(){
    return 'User { _id : $_id, _username : $_username, _photoUrl : $_photoUrl, _bio : $_bio, _city : $_city, _favorite : $_favorite';
  } 

当我使用构造函数创建类的实例并使用调试器观察它时,我感觉我看到了每个变量的重复:私有(private)字段和参数。这正常吗?

enter image description here

最佳答案

嗯,这很正常,您看到的是您的类的表示:所有字段和所有属性。由于您有同名的私有(private)字段和公共(public)属性,因此您会看到它们“两次”,即字段和属性。

顺便说一句,“Dart 方式”这样做就是只拥有公共(public)最终字段。只需将所有字段公开,删除 setter/getter 。我猜你的对象应该是不可变的。所以 make 它是不可变的,使用公共(public) final 字段。

示例:

class User {
  final String id;
  final String username;
  final String photoUrl;
  final String bio;
  final String city;
  final Map<String, dynamic> favorite;

  const User({@required this.id, 
              @required this.username, 
              this.photoUrl, 
              this.bio, 
              this.city, 
              this.favorite
            });

  User copyWith({String id, String username, String photoUrl, String bio, String city, Map<String, dynamic> favorite}){
    return User(
      id: id ?? this.id,
      username: username ?? this.username,
      photoUrl: photoUrl ?? this.photoUrl,
      bio: bio ?? this.bio,
      city: city ?? this.city,
      favorite: favorite ?? this.favorite,
    );
  }

  @override 
  int get hashCode => id.hashCode ^ username.hashCode ^ photoUrl.hashCode ^ city.hashCode ^ bio.hashCode ^ favorite.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is User && runtimeType == other.runtimeType && id == other.id;

  @override 
  String toString(){
    return 'User { id : $id, username : $username, photoUrl : $photoUrl, bio : $bio, city : $city, favorite : $favorite';
  } 

关于flutter - Dart 私有(private)字段和 vscode 调试器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66155680/

相关文章:

listview - 是否可以在 CustomScrollView 中使用 ListView.builder?

dart - Flutter共享首选项验证文件

java - 如何访问不同类中的数组列表

flutter - 如何在Flutter应用中打开pdf,pptx,doc,docx文件?

flutter - 如何具有动态容器大小

flutter - 成员 'setState' 只能在 'package:flutter/src/widgets/framework.dart' 的子类的实例成员中使用

Java-嵌套类的正确使用?

javascript - 为什么删除了错误的对象?

android - 加速度计在运行几分钟后同时触发多次

dart - 带有/不带有关键字 `new`的Dart工厂构造函数,有什么区别?