Bloc: Bloc Listener isn't triggering

Created on 17 Oct 2020  路  20Comments  路  Source: felangel/bloc

Hello!
I'm new to bLoc and I wrote a section in my application with a bloc listener but it isn't triggering as expected. I have attached some images of the code below. Here in the Stream I first call loading state, then I end a try-catch to retrieve data. Once the request is complete and it successfully returns the expected model, I call the success state "AddressFormSuccessfullySaved". I expected this yield to trigger the bloc listener to show the snack bar but it doesn't. The only time it does trigger is when I initially call the loading state. Do you have any suggestions on how I can fix this?聽

I've break pointed each stage but I can't work out why It won't trigger on the success state.

Screenshot 2020-10-17 at 14 10 55

Screenshot 2020-10-17 at 14 11 13

question

All 20 comments

I didn't see the cubit used in the Listener, please use cubit:_yourBlocInstane. Also Please try checking if address value is null in your case.

Thank you for your reply @basnetjiten. But I'm a little confused, Im not using a cubit but in the bloc listener i am referencing the AddressFormBloc. Also the address value isn't null but i will place a if statement for error handling.

Hi @sixseven49 馃憢

Are you extending Equatable on your address model ? If so could you share the code for it ?

Screenshot 2020-10-17 at 14 24 21
The image above shows that address is not null and the success state is being executed.

@narcodico Yes I am :)

You mind sharing the code for the address model ?

import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
part 'address_model.g.dart';

@JsonSerializable()
// ignore: must_be_immutable
class AddressModel extends Equatable{
String id;
String line_1, line_2, city, country, postcode; // address
double latitude, longitude;// address

AddressModel(this.id, this.line_1, this.line_2, this.city, this.country, this.postcode, this.latitude, this.longitude);

@override
List get props => throw UnimplementedError(); // address

factory AddressModel.fromJson(final json) => _$AddressModelFromJson(json);
Map toJson() => _$AddressModelToJson(this);
}

You should mark all your properties with final and pass them to your props.

final String id;
...

@override
List get props => [id, ...]; 

@narcodico Thank you for the suggestion, but the bloc listener is still not triggering on "AddressFormSuccessfullySaved"

Also you can try using BlocObservers for checking the transitions, may be your snackbar context is not working, Trying using return Text("some value) or a print statement to check if that state is reacting.

Feel free to share a minimal reproduction of your issue in a repo.

So I have a address_page then has the blocBuilder and then when state is either AddressFormSuccessfullySaved, AddressFormInitial it will show the address_screen where the blocListener is situated. For the scaffold context I pass that through to the screen widget. Are you saying that context isn't working? because when I first enter the page the AddressFormInitial state triggers the snackbar on load.
Screenshot 2020-10-17 at 14 52 58

Probably your values equality is not properly set up. I can't really tell what you're doing from some incomplete screenshots. Please share a github repository with your issue.

@narcodico can I send a video how whats happening rather than sharing the full repo as it is an application for a company?

If it's a private repo then please create a small example which illustrates your issue and share it. We should be able to run your sample locally, I'm sure your understand that.

Do you have a token saved in shares preferences?
Maybe it is dispatching an AddressFailedToLoad.

@kranfix Thank you for your reply.
yes, Tokens are saved in shared preferences but everything is working and the success staste is being called as expected. But the issue if as I have a page that then contains a bloc builder and depending on the state it will show the screen widget that contains the bloc listener. please see the below images.

Screenshot 2020-10-17 at 16 28 43

Screenshot 2020-10-17 at 16 28 32

@narcodico I made an example flutter app showing the same issue im having how is the best way to present it?

Fixed in this PR.
Your listener can't react if you're placing it in a widget that only gets rendered when state is of certain type, like loaded.
The listener needs to be present in the tree at the time when the new state is emitted.
As a side, there's no point in using callbacks to add events to your bloc, you can directly add them instead of calling a callback. You have access to your bloc in all the sub-tree under the bloc provider.

Was this page helpful?
0 / 5 - 0 ratings