Home > front end >  Flutter Dart - Checking equality between two instances of a class
Flutter Dart - Checking equality between two instances of a class

Time:08-24

I have defined a class for a complex object containing many attributes. I redefined the equality property in this class : an instance of object is equal to another if on attribute is the same.

class CarnetWord {
  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (runtimeType != other.runtimeType) return false;
    return other is CarnetWord && other.idDico == idDico;
  }

  @override
  int get hashCode => idDico.hashCode;

  int idDico;
  int idCarnet;
  int repeatWrong;
  int repeatRight;
  int sayWrong;
  int sayRight;
  int oralTranslateWrongEng;
  int oralTranslateRightEng;
  int writtenTranslateWrongEng;
  int writtenTranslateRightEng;
  int oralTranslateWrongFr;
  int oralTranslateRightFr;
  int writtenTranslateWrongFr;
  int writtenTranslateRightFr;
  int copyRight;
  int copyWrong;
  int writeRight;
  int writeWrong;
  DateTime repeatLast;
  DateTime sayLast;
  DateTime oralTranslateLastEng;
  DateTime writtenTranslateLastEng;
  DateTime oralTranslateLastFr;
  DateTime writtenTranslateLastFr;
  DateTime copyLast;
  DateTime writeLast;
  double wordMastery;

so a CarnetWord is equal to another one if IdDico is the same.

Now when checking this equality : I find myself having to specify all the other attributes in my code. Is this normal ? Or is there a way to just specify IdDico etc.. Below : I have to specify "made up" values for all the other attributes while the only important one is idDico.

Here is the code :

for (int idDico in activite.listeMots) {
      if (_carnetWordBank.contains(CarnetWord(
              idDico: idDico,
              idCarnet: _carnetWordBank.length - 1,
              repeatWrong: 0,
              repeatRight: 0,
              sayWrong: 0,
              sayRight: 0,
              oralTranslateWrongEng: 0,
              oralTranslateRightEng: 0,
              writtenTranslateWrongEng: 0,
              writtenTranslateRightEng: 0,
              oralTranslateWrongFr: 0,
              oralTranslateRightFr: 0,
              writtenTranslateWrongFr: 0,
              writtenTranslateRightFr: 0,
              copyWrong: 0,
              copyRight: 0,
              writeWrong: 0,
              writeRight: 0,
              repeatLast: DateTime.fromMicrosecondsSinceEpoch(0),
              sayLast: DateTime.fromMicrosecondsSinceEpoch(0),
              oralTranslateLastEng: DateTime.fromMicrosecondsSinceEpoch(0),
              writtenTranslateLastEng: DateTime.fromMicrosecondsSinceEpoch(0),
              oralTranslateLastFr: DateTime.fromMicrosecondsSinceEpoch(0),
              writtenTranslateLastFr: DateTime.fromMicrosecondsSinceEpoch(0),
              copyLast: DateTime.fromMicrosecondsSinceEpoch(0),
              writeLast: DateTime.fromMicrosecondsSinceEpoch(0),
              wordMastery: 0)) ==
          true) {
        _list.add(_carnetWordBank
            .firstWhere((element) => element.idDico == idDico)
            .idCarnet);
      } 

In the same way, when I want to "initialize" a new instance of the class : Example : I want _newWord to be an instance of CarnetWord, do I have to specify all the attribute when initializing it ?

Thanks for your help :)

CodePudding user response:

If the attributes in CarnetWord is neither defaulted to a value, nor nullable, then you have to specify all the attributes when you create a new instance of it.

But your loop can be "improved", or at least simplified, without using equality-operator. Consider the equality-operator to be used when you already have two objects and want to check if they are equal.

I'd instead do your loop either like this:

for (int idDico in activite.listeMots) {
  if (_carnetWordBank.where((e) => e.idDico == idDico).isNotEmpty) {
    _list.add(...)
  }
}

Or even better with the help of the collections package:

import 'package:collection/collection.dart';

for (int idDico in activite.listeMots) {
  final carnetWord = _carnetWordBank.firstWhereOrNull((e) => e.idDico == idDico)
  if (carnetWord != null) {
    _list.add(carnetWord.idCarnet)
  }
}
  • Related