Skip to content

Freezed when method isn't recognized β€” help needed #4350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Mohamad-Ammis opened this issue May 2, 2025 · 2 comments
Closed

Freezed when method isn't recognized β€” help needed #4350

Mohamad-Ammis opened this issue May 2, 2025 · 2 comments
Labels
request Requests to resolve a particular developer problem

Comments

@Mohamad-Ammis
Copy link

Hey folks πŸ‘‹
I'm trying to use freezed for the first time to handle my Cubit states, but I can't access the generated methods like when, maybeWhen, etc. The error I get is:


βœ… What I did

πŸ“„ My AuthState class

part of 'auth_cubit.dart';

@freezed
class AuthState with _$AuthState {
  const factory AuthState.initial() = _Initial;
  const factory AuthState.registerLoading() = _RegisterLoading;
  const factory AuthState.registerFailure({required String errMessage}) = _RegisterFailure;
  const factory AuthState.registerSuccess() = _RegisterSuccess;
}

πŸ“„ My `AuthCubit class

import 'dart:developer';
import 'package:delta_app/core/bloc/base_cubit.dart';
import 'package:delta_app/core/utils/service_locator.dart';
import 'package:delta_app/feature/auth/data/models/register_input_model.dart';
import 'package:delta_app/feature/auth/data/repos/auth_repository.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'auth_state.dart';
part 'auth_cubit.freezed.dart';

class AuthCubit extends BaseCubit<AuthState> {
  AuthCubit() : super(AuthState.initial());

  final AuthRepository authRepository = getIt.get<AuthRepository>();

  Future<bool> register({required RegisterInputModel model}) async {
    try {
      bool result = false;
      safeEmit(AuthState.registerLoading());

      final data = await authRepository.register(model: model);

      data.fold(
        (failure) => safeEmit(AuthState.registerFailure(errMessage: failure.errorMessage)),
        (success) {
          safeEmit(AuthState.registerSuccess());
          result = true;
        },
      );

      return result;
    } catch (e) {
      log('e: $e');
      safeEmit(AuthState.registerFailure(errMessage: e.toString()));
      return false;
    }
  }
}

😒 where i try to use when

BlocBuilder<AuthCubit, AuthState>(
  builder: (context, state) {
    return CustomButton(
      onTap: () {
        if (_key.currentState!.validate()) {
          log('validate');
        } else {
          setState(() {
            _autoValidateMode = AutovalidateMode.onUserInteraction;
            _key.currentState!.save();
          });
        }
      },
      gradient: kCommonGradinet,
      child: Text(
        "Ψ₯Ω†Ψ΄Ψ§Ψ‘ Ψ­Ψ³Ψ§Ψ¨",
        style: Styles.style15Bold.copyWith(color: Colors.white),
      ),
    );
  },
);

what i've tried

remove all dependencies and add them again

delete pubspec.lock and run flutter clean ,flutter pub get then run build runner again

Ran flutter pub run build_runner build --delete-conflicting-outputs

add this to analysis_options.yaml

analyzer:
  errors:
    invalid_annotation_target: ignore

Tried hot restart and full rebuild

@Mohamad-Ammis Mohamad-Ammis added the request Requests to resolve a particular developer problem label May 2, 2025
@Mohamad-Ammis Mohamad-Ammis reopened this May 2, 2025
@srawlins
Copy link
Member

srawlins commented May 2, 2025

I think you're looking to file an issue against the freezed package? This repository is for discussions regarding the Dart language.

@Mohamad-Ammis
Copy link
Author

Sorry I didn't notice that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants