Home > Software design >  Why get props using in class on dart?
Why get props using in class on dart?

Time:09-11

I used equatable in flutter dart to generate some boilerplate codes. here is a class that generated some boilerplate code using equitable

import 'package:equatable/equatable.dart';

class Task extends Equatable {
  final String title;
  bool? isDone;
  bool? isDeleted;

  Task({
    required this.title,
    this.isDone,
    this.isDeleted,
  }) {
    isDone = isDone ?? false;
    isDeleted = isDeleted ?? false;
  }

  Task copyWith({
    String? title,
    bool? isDone,
    bool? isDeleted,
  }) {
    return Task(
      title: title ?? this.title,
      isDone: isDone ?? this.isDone,
      isDeleted: isDeleted ?? this.isDeleted,
    );
  }

  Map<String, dynamic> toMap() {
    final result = <String, dynamic>{};

    result.addAll({'title': title});
    if (isDone != null) {
      result.addAll({'isDone': isDone});
    }
    if (isDeleted != null) {
      result.addAll({'isDeleted': isDeleted});
    }

    return result;
  }

  factory Task.fromMap(Map<String, dynamic> map) {
    return Task(
      title: map['title'] ?? '',
      isDone: map['isDone'],
      isDeleted: map['isDeleted'],
    );
  }

  @override
  List<Object?> get props => [
        title,
        isDeleted,
        isDone,
      ];
}

and my question is why is this code and what is this code doing? I read some documents but I didn't find the exact answer. the docs say it is used to compare with another class but why do we need to compare? also, I'm not writing any other class. so which classes are being compared? or is it not used to compare??

 @override
  List<Object?> get props => [
        id,
        title,
        isDeleted,
        isDone,
      ];

what's the purpose of this line? also, why are we using Object class?

CodePudding user response:

This is to specify which property to compare to check if an instance is equal to the other instance of the same class. This is useful because if there is a property we want or do not want to compare we can add or remove it to the returned array.

In this example, this code compares if the first instance of a Task is equal to the second instance of a Task.

main() {
    var task1 =  Task(title: 'task 1', isDone: true, is Deleted: true);
    var task2 =  Task(title: 'task 2', isDone: false, isDeleted: false);
    print(task1 == task2); // false
}

It returns List<Object> because the class Task is extending Equatable, so Task needs to override List<Object?> get props.

This function compares string and boolean. Keep in mind that string and boolean are both an Object in dart, so if you are comparing only string or only boolean you can return List<String> or List<bool>.

CodePudding user response:

but why do we need to compare?

you dont have to compare any class if you dont need it.

so dont make your self confuse.


but when do we need it?

eg: when we need to render widget with some condition. we need to compare 2 instaces.

final user1 = User (name: "poo", role: "manager");
final user2 = User( name: "boo", role: "engineer");

let say we want render container if role is manager

User.role == "manager" ? Container() : Text("this only show on manager level"),

actually we can write the code manually to make the behavior of comparison works fine, but we can also use the package to make it auto generate. so the condition works as we want.


what code we need to write manually ?

this one:

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

  @override
  int get hashCode => name.hashCode;

we need to write the code if we want to compare title. Task("title1") == 'title1'

another props like: id (if we need to compare them later). Then we need to write another code for id,.

also do it for all your props if you want to compare them later.

what actually Equatable do is to generate thats code. it will generate this for all your props.

@override
  List<Object?> get props => [
        id,
        title,
        isDeleted,
        isDone,
      ];

Now you can compare you id, title, isDeleted, isDone without write code comparison manually.

  • Related