'Is it possible to assign a property value and return the original object?

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);


Solution 1:[1]

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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jamesdlin