0

Im currently working on a Flutter school project. Im totaly new to Flutter and Dart and have troubles to understand the usage of Equatable package.

I build my Flutter Application using the Bloc-Pattern. In an online course they described the usage of Equatable, and how it helps to make comparison of objects more reliable. But since equatable objects also need to be immutable i dont understand how to work with those objects.

I have the following scenario. Inside my Flutter project i have a Edit-View to change, lets say, AccountInformations. AccountInformations are stored to a DB using Asp.Net backend. In Flutter i call the service to get the AccountInformation, which is also a model class in the Flutter project. The AccountInformation is Equatable since i want to compare it to differen AccountInformation objects.

Now i plan to bind the properties/variables of the AccountInformation-object to the TextFields in the Edit-View. But since the properties are final, i cannot change them.

Now i just see the option, to have a method and create a new AccountInfo object based on the text-fields on "Save" to write it back to the database, or to not make the AccountInfo euqatable.

In my opinion this is way to complicated and im pretty sure that im missing an important part. How would you realize the folowing scenario in your application?

Thanks and regards!

Joker
  • 11
  • 1
  • "equatable objects also need to be immutable", no they don't. so almost everything you conclude after that is confusing. :) – Randal Schwartz Aug 22 '23 at 17:12
  • Please refer this link as it explains the usage of equatable with good examples :https://medium.flutterdevs.com/equatable-in-flutter-ebb1e411bd5f – Sagar Acharya Aug 30 '23 at 09:53

1 Answers1

0

You can use copyWith method, using the copyWith method is a great approach to handle scenarios like this. It allows you to create a new object with updated properties while keeping the original object immutable. Here's a short example of how you can use copyWith in your scenario:

Let's assume you have an AccountInformation class that looks something like this:

import 'package:equatable/equatable.dart';

class AccountInformation extends Equatable {

  const AccountInformation({
    required this.username,
    required this.email,
    required this.phoneNumber,
  });

  final String username;
  final String email;
  final String phoneNumber;

  @override
  List<Object?> get props => [username, email, phoneNumber];

  AccountInformation copyWith({
    String? username,
    String? email,
    String? phoneNumber,
  }) {
    return AccountInformation(
      username: username ?? this.username,
      email: email ?? this.email,
      phoneNumber: phoneNumber ?? this.phoneNumber,
    );
  }
}

With the copyWith method defined in the AccountInformation class, you can easily create a new instance with updated properties. Here's an example of how you can use it in your Flutter code:

// Assume you have an existing accountInfo object
AccountInformation oldAccountInfo = /* get the account info */;

// Create a new instance with updated properties
AccountInformation newAccountInfo = oldAccountInfo.copyWith(
  username: 'newUsername', // updated username
  email: 'newEmail',       // updated email
);

// Now you can use the newAccountInfo object to update the UI or send it back to the database

I hope this solution is helpful.

Sami
  • 96
  • 9