'LoginBloc' doesn't extend 'Cubit<LoginState>'

Viewed 2176

I'm new in flutter. currently I used bloc to help me. When I'm used bloc in UI, I'm always got 'LoginBloc' doesn't extend 'Cubit<LoginState>' I'm used dependencies flutter_bloc Here my UI

 @override
Widget build(BuildContext context) {
 return Scaffold(
  body: BlocBuilder<LoginBloc, LoginState>(
    builder: (BuildContext context, LoginState state) {},
   ),
 );
 }

here my bloc class.

class LoginBloc extends Bloc<LoginEvent, LoginState> {
final AuthenticationService authenticationService;

LoginBloc(this.authenticationService) : super(null);

@override
Stream<LoginState> mapEventToState(LoginEvent event) async* {
 if (event is LoginSubmitted) {
  yield* _loginMapToState(event);
 }
}

Stream _loginMapToState(LoginSubmitted event) async* {
try {
  UserDTO userDTO =
      UserDTO(event.username, event.password, event.rememberMe);
  var response =
      await authenticationService.authenticate(userDTO.toJson().toString());
  if (response.statusCode == 200) {
    Map<String, String> token = jsonDecode(response.body);
    await Preferences.setString(ConstValue.TOKEN, token['id_token']);
    yield LoginSuccess();
   }
  } catch (e) {
  yield LoginError(e);
 }
 }
}
4 Answers

You need to either use cubit or bloc. You are using bloc here and extending cubitState which is not feasible. Try extending BlocState instead of CubitState. Also, share the events and states part of the bloc code, which may help debug your code.

The issue will likely be in your Bloc Event Class. As an example, if you have the class below, it will likely cause that error

class GetTrackStages extends StagesEvent {
  final String trackId;

 GetTrackStages({@required this.trackId});
}

If you have the class below instead, you won't have that issue

class GetTrackStages extends StagesEvent {
  final String trackId;

  const GetTrackStages({@required this.trackId});
}

All you need to do is to add a const to your event constructor.

In my case, I mistake on BlocBuilder parameter. First one always is Bloc and 2nd is always State

enter image description here

only change file name in bloc folder

ex - [ productCategoryList_Bloc.dart ] to [ list_bloc.dart ]

ex - [ articleMyListEvent_Bloc.dart ] to [ article_event.dart ]

Related