Bloc: BlocBuilder is not calling build

Created on 25 Sep 2019  路  3Comments  路  Source: felangel/bloc

I am using bloc to load a list of products, but the Build inside bloc builder is not being called when I yield the same state with different list of products.

So when I am already on PoductLoadedState, and I call _mapProductQuantityUpdatedToState
a new PoductLoadedState is being yield, but the builder inside the BlocBuilder is not being called.

This is my Screen:
`class ProductListScreen extends StatelessWidget {
final Repository _repository;

ProductListScreen(this._repository);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Test",
style: TextStyle(color: Colors.black),
),
),
body: BlocProvider(
builder: (context) =>
ProductsBloc(_repository)..dispatch(ProductsStart()),
child: BlocBuilder(
builder: (context, state) {
if (state is ProductsLoadedState) {
return buildList(state.products);
}
return Center(child: CircularProgressIndicator());
},
)));
}`

This is the bloc:

Stream<ProductsState> _mapProductQuantityUpdatedToState(ProductQuantityUpdated event) async* { _repository.updateProduct( event.product.product.id, event.quantity, event.product.measure); if (currentState is ProductsLoadedState) { var state = (currentState as ProductsLoadedState); var oldProducts = List.from(state.products); var index = oldProducts.indexWhere((item) { return item.product.id == event.product.product.id; }); oldProducts[index] = SelectedProduct( event.product.product, event.quantity, event.product.measure); yield ProductsLoadedState(oldProducts); } }

This is the State:
`@immutable
abstract class ProductsState extends Equatable {
ProductsState([List props = const []]) : super(props);
}

@immutable
class InitialProductsState extends ProductsState {
@override
String toString() => 'InitialProductsState';
}

@immutable
class ProductsLoadedState extends ProductsState {
final List products;
ProductsLoadedState(this.products): super([products]);

@override
String toString() => 'ProductsLoadedState , ${products.length} products on the list';
}
`

And this my models:

`class Product extends Equatable {
final String _name;
final String _price;
final String _imageUrl;
final String _id;

Product(this._name, this._price, this._imageUrl, this._id) : super([_name, _price, _imageUrl, _id]);

String get name => _name;

String get price => _price;

String get imageUrl => _imageUrl;

String get id => _id;
}

class SelectedProduct extends Equatable {
final Product _product;
final double _quantity;
final Measure _measure;

SelectedProduct(this._product, this._quantity, this._measure)
: super([_product, _quantity, _measure]);

Product get product => _product;

double get quantity => _quantity;

Measure get measure => _measure;
}
`

question

Most helpful comment

Thanks, I think it was the problem the dynamic types.
Thanks for answering.

All 3 comments

Hi @themanol 馃憢
Thanks for opening an issue!

If I had to guess, it looks like the final List products; in your ProductsLoadedState is of type dynamic. Can you please update it to have an explicit type like:

class ProductsLoadedState extends ProductsState {
  final List<Product> products;
  ProductsLoadedState(this.products): super([products]);
...

Also, make sure whatever type you're using is also extending Equatable and passing all properties to the superclass.

If that doesn't help then can you please provide a link to a sample application which illustrates the problem you're having? It would be much easier to help if I can reproduce the problem you're having locally. Thanks! 馃憤

Closing for now but feel free to comment with additional questions/comments and I'm happy to continue the conversation 馃憤

Thanks, I think it was the problem the dynamic types.
Thanks for answering.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

timtraversy picture timtraversy  路  3Comments

krusek picture krusek  路  3Comments

Reidond picture Reidond  路  3Comments

wheel1992 picture wheel1992  路  3Comments