Search code examples
flutterdartvalidationflutter-dependencies

The argument type 'bool' can't be assigned to the parameter type 'FormzSubmissionStatus?'


I am trying to implement form validation using the Formz package but I'm getting the above error on my controller, I can't seem to figure it out. Please anybody knows a fix for this? Help me!! The error is coming from the "status: Formz.validate([])"

import 'package:flymedia_app/src/auth/signup/controller/signup_state.dart';
import 'package:form_validators/form_validators.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class SignupController extends StateNotifier<SignUpState> {
  SignupController() : super(const SignUpState());

  void onNameChange(String value) {
    final name =  Name.dirty(value);
    state = state.copyWith(
      name: name,
      status: Formz.validate([
        name,
        state.email,
        state.password,
      ]),
    );
  }



  void signUpWithEmailAndPassword() async {
    if (!state.status.isValidated)  return;
      print('Signing up...');
  }

}

Also here "if (!state.status.isValidated) return;" I'm getting "The getter 'isValidated' isn't defined for the type 'FormzSubmissionStatus'."

This is my Signup State import 'package:equatable/equatable.dart'; import 'package:form_validators/form_validators.dart';

class SignUpState extends Equatable {
  final Name name;
  final Email email;
  final Password password;
  final FormzSubmissionStatus status;
  final String? errorMessage;

  const SignUpState({
    this.name = const Name.pure(),
    this.email = const Email.pure(),
    this.password = const Password.pure(),
    this.status = FormzSubmissionStatus.initial,
    this.errorMessage,
  });

  SignUpState copyWith({
    Name? name,
    Email? email,
    Password? password,
    FormzSubmissionStatus? status,
    String? errorMessage,
  }) {
    return SignUpState(
      name: name ?? this.name,
      email: email ?? this.email,
      password: password ?? this.password,
      status: status ?? this.status,
      errorMessage: errorMessage ?? this.errorMessage,
    );
  }

  @override
  List<Object> get props =>
      [
        name,
        email,
        password,
        status,
      ];
}

Solution

  • You have messed with types. Formz.validate returns bool, but your status field is FormzSubmissionStatus enum. So you have to change it's type, or add another field, for example isValid and assign Formz.validate result to it.