How to define proper state class in bloc while using freezed plugin

Viewed 101

I am trying to understand, how can I use the bloc pattern (specifically state) properly. I am facing this issue for more than a month, but not able to figure out a solution.

Let's consider, I have defined state class following way,



@freezed
abstract class ExampleState with _$ExampleState {
  const factory ExampleState.initial() = Initial; 

  const factory ExampleState.getDataFromServer() = GetDataFromServer; //Thsi will return the ServerData Object.

  const factory ExampleState.useServerData() = UseServerData;

  const factory ExampleState.changeServerDataAndUpload() = ChangeServerDataAndUpload;
}

Let's consider our Server Data Model is the following way

class ServerData {
  final String userId;
  final String firstName;
  final String lastName;
  final String fullAddress;
  final String fatherName;

  ServerData(
    this.userId,
    this.firstName,
    this.lastName,
    this.fullAddress,
    this.fatherName,
  );
}


In this example, we are able to see, GetDataFromServer, UseServerData, and ChangeServerDataAndUpload state is sharing the same ServerData object. How should I design my state such that, the same DataModel object can be shared between different states?

0 Answers
Related