bloc icon indicating copy to clipboard operation
bloc copied to clipboard

issue: type 'Null' is not a subtype of type '<BLOC_NAME>' in type cast, relate to repository injection

Open phamducmanh1452001 opened this issue 2 years ago • 2 comments

Hi everyone at the great state management ! I did refactor my code from #photo1 to #photo2, and I receive Error: "type 'Null' is not a subtype of type 'LoginBloc' in type cast".

The named "create" function in BlocProvider did not call. For my part, I see they are similar but flutter_bloc said no. How do they work or effect to Flutter tree then create this difference ?

Thank you so much and happy coding !

#photo1 image

#photo2 image

phamducmanh1452001 avatar Aug 16 '22 08:08 phamducmanh1452001

These two codes will not behave equally. The closure passed "create" is, by default, executed lazily, which makes, in the first code snippet, the repository instances to be instantiated only when referenced for the first time. But in the refactored code, these same instancies are created eagerly.

But the interesting part is that "LoginBloc" (as described in the error message) is not part of the refactored code.

renancaraujo avatar Aug 16 '22 11:08 renancaraujo

These two codes will not behave equally. The closure passed "create" is, by default, executed lazily, which makes, in the first code snippet, the repository instances to be instantiated only when referenced for the first time. But in the refactored code, these same instancies are created eagerly.

But the interesting part is that "LoginBloc" (as described in the error message) is not part of the refactored code.

I don't think the refactored code make them are created eagerly, buddy. But maybe, I guess constructor of RepositoryProvider did change a global thing

phamducmanh1452001 avatar Aug 17 '22 02:08 phamducmanh1452001

Hi @phamducmanh1452001

I face the same problem today. So I solve them by wrapping Multi Bloc Provider in Multi Repository Provider.

code

3kdeveloper avatar Nov 22 '22 15:11 3kdeveloper

The flutter_login demo has this problem : type 'Null' is not a subtype of type 'LoginBloc' in type cast

Allan-Nava avatar Nov 24 '22 10:11 Allan-Nava

@phamducmanh1452001 can you please provide a link to a minimal reproduction sample? This isn't enough information to help unfortunately.

felangel avatar Dec 27 '22 01:12 felangel

I am getting the same error with flutter_login demo

emperorKez avatar Dec 30 '22 00:12 emperorKez

I am getting the same error with flutter_login demo

I'm not able to reproduce. Can you please provide clear reproduction steps along with the full error and stacktrace? Thanks!

felangel avatar Jan 02 '23 22:01 felangel

Closing for now since I can’t reproduce and there is no reproduction sample. Feel free to provide one and I’m happy to take another look 👍

felangel avatar Jan 12 '23 14:01 felangel

@felangel Hi bud, I am facing the same error i am sharing the traceback

image

this is the logi_page.dart file snippet I have imported login_form.dart since the LoginForm widget needed it image

dk-a-dev avatar May 31 '24 14:05 dk-a-dev

import 'dart:async';

import 'package:authentication_repository/authentication_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_login/login/models/models.dart';
import 'package:formz/formz.dart';

part 'login_event.dart';
part 'login_state.dart';

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  LoginBloc({
    required AuthenticationRepository authenticationRepository,
  })  : _authenticationRepository = authenticationRepository,
        super(const LoginState()) {
    on<LoginUsernameChanged>(_onUsernameChanged);
    on<LoginPasswordChanged>(_onPasswordChanged);
    on<LoginSubmitted>(_onSubmitted);
  }

  final AuthenticationRepository _authenticationRepository;

  void _onUsernameChanged(
    LoginUsernameChanged event,
    Emitter<LoginState> emit,
  ) {
    final username = Username.dirty(event.username);
    emit(
      state.copyWith(
        username: username,
        isValid: Formz.validate([state.password, username]),
      ),
    );
  }

  void _onPasswordChanged(
    LoginPasswordChanged event,
    Emitter<LoginState> emit,
  ) {
    final password = Password.dirty(event.password);
    emit(
      state.copyWith(
        password: password,
        isValid: Formz.validate([password, state.username]),
      ),
    );
  }

  Future<void> _onSubmitted(
    LoginSubmitted event,
    Emitter<LoginState> emit,
  ) async {
    if (!state.isValid) {
      return;
    }
    emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
    try {
      await _authenticationRepository.logIn(
        username: state.username.value,
        password: state.password.value,
      );
      emit(state.copyWith(status: FormzSubmissionStatus.success));
    } catch (_) {
      emit(state.copyWith(status: FormzSubmissionStatus.failure));
    }
  }
}

this is login_bloc.dart

dk-a-dev avatar May 31 '24 14:05 dk-a-dev