user54517
user54517

Reputation: 2420

Dart private fields & vscode debugger

Here is a class with private fields and the constructor :

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';
  } 

When I use the constructor to create an instance of the class, and that I observe it with the debugger, I feel like I see a duplication of each variable : the private field and the argument. Is that normal ?

enter image description here

Upvotes: 2

Views: 242

Answers (1)

nvoigt
nvoigt

Reputation: 77354

Well, it is normal, what you see is a representation of your class: all fields and all properties. Since you have private fields and public properties of the same name, you see them "twice", both fields and properties.

By the way, the Dart way" of doing this would be to just have public final fields. Just make all your fields public, remove the getters. Your object is supposed to be immutable I guess. So make it immutable, using public final fields.

Example:

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';
  } 

Upvotes: 2

Related Questions