user16906111
user16906111

Reputation:

non null-able variable have to be initialized in both of the constructors

I need to create two private constructors with two different variables. But non null-able variables have to be initialized in both of the constructors. But I don't want so.

The Code Is:

  class Password extends ValueObject<SignInValueFailures> {
      Password._(this.value);
      Password.__(this.confirmPass,this.value);

      @override
       Either<SignInValueFailures, String> value; //==> i don't need it inside the second constructor Password.__()
      
       Either<SignUpValueFailures, String>? confirmPass; 
    }

Value Failure Class:

abstract class ValueObject<T> {
  Either<T, String> get value;
  getOrCrash(){
    return value.fold((l) => throw UnhandledError(), (r) => r);
  }

  getOrElse(String Function() dflt){
    return value.getOrElse(dflt);
  }
  
  @override
  String toString() => 'ValueObject(value: $value)';

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is ValueObject && other.value == value;
  }

  @override
  int get hashCode => value.hashCode;
}

Upvotes: 1

Views: 69

Answers (1)

Soniya
Soniya

Reputation: 11

If you need to initialize either of the values, I'd suggest to convert the variables to late. This tells compiler to that these variables would be initialized before using.

Example:

class Password extends ValueObject<SignInValueFailures> {
      Password._(this.value);
      Password.__(this.confirmPass,this.value);

      @override
       late Either<SignInValueFailures, String> value; 
      
       late Either<SignUpValueFailures, String> confirmPass; 
}

Upvotes: 1

Related Questions