alexeieleusis / greencat

A port of Redux(https://github.com/reactjs/redux) to Dart, including Redux Thunk(https://github.com/gaearon/redux-thunk) and a simple Logger.
Apache License 2.0
48 stars 4 forks source link

Unable to set value to null #14

Closed theobouwman closed 7 years ago

theobouwman commented 7 years ago

In the Flutter example it is not possible to set a property to null.

AuthState copy({GoogleSignInAccount googleSignInAccount}) => new AuthState(googleSignInAccount ?? this.googleSignInAccount);

I want to do this because I listen to the changes. But since it'll take the object's property when the googleSignInAccount value is not defined or null this is not possible.

I am wondering how I can accomplish this?

alexeieleusis commented 7 years ago

I think that you want to get rid of the ?? or extend it to a ternary operator ? : when you detect the cases where you indeed want to set it to null.

theobouwman commented 7 years ago

@alexeieleusis when I use googleSignInAccount == null ? null : googleSignInAccount it will set the value to null when I set another property and leave the googleSignInAccount value empty.

In JavaScript ES6 you can use the ... operator to copy all properties and override only the those you want to change.

Do you understand me?

alexeieleusis commented 7 years ago

I would need to look at the actual code or a more comprehensive example, I guess you have other flag besides the googleSignInAccount parameter being null that lets you determine what value you should return. A pattern I used in the example is to have a copy constructor, I did it in a way that didn't accept null values because is a small and simple app and there is no need to deal with that, but I can do it mostly because it is not a real world app and what I want to exemplify is the store itself.

theobouwman commented 7 years ago

@alexeieleusis well this is my State:

class AuthState {
  final FirebaseUser firebaseUser;

  final String token;

  final List<Map> contracts;
  final bool loadingContracts;
  final String contractsError;

  final List<Map> searchModules;
  final bool loadingSearchModules;
  final String searchModulesError;

  AuthState(this.firebaseUser, this.token, this.contracts,
      this.loadingContracts, this.contractsError, this.searchModules,
      this.loadingSearchModules, this.searchModulesError);

  const AuthState.initial()
      : this.firebaseUser = null,

        this.token = null,

        this.contracts = null,
        this.loadingContracts = false,
        this.contractsError = null,

        this.searchModules = null,
        this.loadingSearchModules = false,
        this.searchModulesError = null;

  AuthState copy({
    FirebaseUser firebaseUser,
    String token,

    List<Map> contracts,
    bool loadingContracts,
    String contractsError,

    List<Map> searchModules,
    bool loadingSearchModules,
    String searchModulesError
  }) => new AuthState(
      firebaseUser ?? this.firebaseUser,

      token ?? this.token,

      contracts ?? this.contracts,
      loadingContracts ?? this.loadingContracts,
      contractsError ?? this.contractsError,

      searchModules == null ? null : searchModules,
      loadingSearchModules ?? this.loadingSearchModules,
      searchModulesError ?? this.searchModulesError
  );
}

This is what I want: .copy(token: null) to set ONLY the token value to null. But I also want this: .copy(contracts: null, loadingContracts: false) to set ONLY thecontractsto null and theloadingContractstofalse`.

I am unable to do this with the current code because it will take the object's property if the given property is null.

So how can I accomplish this?

alexeieleusis commented 7 years ago

I think that this pattern does not suit your needs, you would need to remove the ?? operator where you want properties to allow for null and probably change the method name if you think it no longer conveys what you want.