Bloc: Previous state is same as current state in blocbuilder after update

Created on 8 Sep 2020  路  5Comments  路  Source: felangel/bloc

Hello I'm relatively new to the bloc pattern in flutter.

I have a simple listing app, when I make change in the the bloc of my model state, Blocbuilder in my widget see the change of state, but it didn't see any change at the model state as either previous and current state have the same new value that I changed.

My model
````
class ListingForm {
final List imageFormList ;
final List categoryFormList;
const ListingForm({
this.imageFormList = const [] ,
this.categoryFormList = const [],
});

}
````

My State
````
class ListingFormState extends Equatable {
const ListingFormState({
this.listingForm = const ListingForm() ,
});

final ListingForm listingForm;

ListingFormState copyWith({
ListingForm listingForm,
}) {
return ListingFormState(
listingForm: listingForm ?? this.listingForm,
);
}

@override
List get props => [listingForm ];
}

class ListingFormLoaded extends ListingFormState{

final ListingForm listingForm;

ListingFormLoaded(this.listingForm) ;

@override
List get props => [listingForm];
}
````

Part of my bloc
````
else if (event is AddNewImage) {
final ListingForm listingForm = state.listingForm ;
listingForm.imageFormList.add(new ImageForm(image: event.imageForm.image));

  yield ListingFormLoaded( listingForm);

}
````

I know that I might wrong with some logic, I already do a lot of research, any help will be helpful

question

Most helpful comment

HI @narcodico
Thanks for taking your precious time to help me,
I learned from your PR, you did more than solve an issue for me
Thanks again !

All 5 comments

Hi @moisedestin 馃憢
Thanks for opening an issue!

In order for Equatable to work properly you need to also make ListingForm extend Equatable.

Closing for now but if you're still having trouble after making ListingForm extend Equatable then please create a sample app which illustrates the issue and I'm happy to take a look and open a pull request with suggestions 馃憤

Thanks @felangel for the reply.
It didn't work

here's the simple app repo
https://github.com/moisedestin/flutter_blocexample

Hello @felangel !
I still can't figure it out, it might take you a minute maybe.

Hi @moisedestin 馃憢

I've solved your issues in this PR.
Please avoid storing widgets directly in your models. The purpose of a model(view model if you prefer) is to store data and metadata for your UI, but not the actual UI 馃槉. A big problem of storing something like that into your model is that you don't have access to that class to change how the equality should behave. Basically you can't extend Equatable on an Image for example.

When working with lists as part of your bloc's state please ensure you're always yielding a new list instead of mutating the previous one.

I've also did some improvements to your UI widgets and showcased how you should DI your dependencies into your blocs.

Hope that helps 馃憤

HI @narcodico
Thanks for taking your precious time to help me,
I learned from your PR, you did more than solve an issue for me
Thanks again !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

frankrod picture frankrod  路  3Comments

timtraversy picture timtraversy  路  3Comments

abinvp picture abinvp  路  3Comments

krusek picture krusek  路  3Comments