Home > Net >  Is it possible to assign a property value and return the original object?
Is it possible to assign a property value and return the original object?

Time:04-26

I want to avoid writing these three lines over and over in different functions.

var newState = state.copy();
newState.user.googleUser = googleUser;
state = newState;

Something like this?

state = (state.copy().user.googleUser = googleUser);

CodePudding user response:

Use the cascade operator:

state = state.copy()..user.googleUser = googleUser;

Alternatively, a common pattern is to instead create a copyWith method for your classes so that you can do:

state = state.copyWith(user: googleUser);
  •  Tags:  
  • dart
  • Related