I have a state with multiple variables
im changing the state with setters state but every time i change the state with setter state variables reset to initial state
here is my state
const AuthSignInState({
this.signUpType = 'email',
this.errorMessage = '',
this.errorSignIn = false,
this.isLoading = false,
this.currentUser = '',
this.user,
});
final String errorMessage;
final bool errorSignIn;
final String signUpType;
final bool isLoading;
final String currentUser;
final Sync? user;
}
here is my setter state for example i want to change the signuptype when i trigger this state other variables will reset to the initial value how can i persist the state even after setting one variable
class SetSignInTypeState extends AuthSignInState {
final String type;
const SetSignInTypeState({required this.type}) : super(signUpType: type);
@override
List<Object?> get props => [type];
}
2
Answers
It is because the setter state is creating a new instance of the state class with the updated values. This means that the old instance of the state class is discarded and all of its values are lost.
You can use
ChangeNotifier
orStreamController
to persist the state even after setting one variable.By
ChangeNotifier
In this code, the
setSignUpType()
method sets thesignUpType
variable and then calls thenotifyListeners()
method to notify its listeners that the state has changed. This ensures that the UI will be updated to reflect the new value of thesignUpType
variable.By
StreamController
In the above code, the
setSignUpType()
method sets thesignUpType
variable and then adds a new event to the_controller
stream. This event will be emitted to all of the listeners of the stream, which will then update their UI accordingly.Let me know if this helps.
To preserve all values and change only one (or few) of them, it’s common to use methods like
copyWith
. Then you create a new instance of an object with all the values of the old one, with changes specified in arguments of that function. It is made very easy with freezed package (https://pub.dev/packages/freezed). Checkout it on pub.dev and also here is Flutter’s team short explanation of this package’s capabilities https://youtu.be/RaThk0fiphAIn general the implementation looks like this:
Then, after generating your code with build_runner, you would have in your bloc:
Freezed is an efficient and less time consuming solution to change properties of an object. Hope that helps.