How to fix an error in cubits state(Too many positional arguments: 0 expected, but 1 found)?

Issue

I am trying to find out how to use bloc/cubits in flutter but I’ve met a problem with state arguments.

Also I use freezed and freezed_annotation for models in auth

This is my cubits file:

import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:productivity/models/auth/auth.dart';

part 'auth_state.dart';

class AuthCubit extends Cubit<AuthState> {
  AuthCubit() : super(AuthState(
    Auth( // Here I get this error: Too many positional arguments: 0 expected, but 1 found.
      email: "",
      password: "",
      firstName: "",
      lastName: "",
      genderId: 0,
      ageGroupId: 0,
      countryUuid: 0
    ) // Auth comes from models/auth.dart
  ));

  void setCountryUuid(int countryUuid) => emit(state.copyWith(countryUuid: countryUuid));

}

But if I try to fix it using IDe(Add required positional parameter) then my state file is getting broken

This is my state:

part of 'auth_cubit.dart';

class AuthState extends Equatable {
  final String? email;
  final String? password;
  final String? firstName;
  final String? lastName;
  final int? genderId;
  final int? ageGroupId;
  final int? countryUuid;

  const AuthState({
    this.email,
    this.password,
    this.firstName,
    this.lastName,
    this.genderId,
    this.ageGroupId,
    this.countryUuid,
  });

  AuthState copyWith({
    String? email,
    String? password,
    String? firstName,
    String? lastName,
    int? genderId,
    int? ageGroupId,
    int? countryUuid,
  }) {
    return AuthState( // 1 positional argument(s) expected, but 0 found.
      email: email ?? this.email,
      password: password ?? this.password,
      firstName: firstName ?? this.firstName,
      lastName: lastName ?? this.lastName,
      genderId: genderId ?? this.genderId,
      ageGroupId: ageGroupId ?? this.ageGroupId,
      countryUuid: countryUuid ?? this.countryUuid,
    );
  }

  @override
  List<Object?> get props =>
      [email, password, firstName, lastName, genderId, ageGroupId, countryUuid];
}

And this is my models/auth/auth.dart file:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'auth.freezed.dart';

part 'auth.g.dart';

@freezed
class Auth with _$Auth {
  factory Auth({
    String? email,
    String? password,
    String? firstName,
    String? lastName,
    int? genderId,
    int? ageGroupId,
    int? countryUuid
  }) = _Auth;

  factory Auth.fromJson(Map<String, dynamic> json) => _$AuthFromJson(json);
}

Solution

You are passing Auth inside AuthState. For AuthState cubit, it will be

class AuthCubit extends Cubit<AuthState> {
  AuthCubit() : super(AuthState(
      email: "",
      password: "",

For Auth cubit, it will be

class AuthCubit extends Cubit<Auth> {
  AuthCubit() : super(Auth(
      email: "",
      password: "",

Answered By – Yeasin Sheikh

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *