hesham shawky
hesham shawky

Reputation: 1151

Flutter: Do I have to create a bloc for each part on my app? What it the best practices?

I'm developing an E-commerce Mobile app, using flutter with Bloc as a Design pattern and flutter_bloc package for simplifying things.

My question is: do I have to create a bloc of each part of my app? is this the best practice for using the BLOC as a design pattern?

For example, at my App on the Home Screen there are some sections like best sellers and offers ...etc, do I have to create a separate bloc for each section? as I guess that way will be there a lot of repeated code as well a lot of files and still can't do it all at once using the same bloc logically.

Upvotes: 0

Views: 457

Answers (1)

user12195344
user12195344

Reputation:

do you got sellers from api ? if you do you can use one bloc for all of them .....

in state you gotta say

    class SellerInfoLoaded extends SellerState {
  final List<sellerInfoModel> bestsellers;
  final List<sellerInfoModel> badsellers;

  OrderLoaded({ @required this.bestsellers, @required this.badsellers,});
  @override
  List<Object> get props => [bestsellers,badsellers];
}

then in the bloc you gotta say

if (event is fetchseller) {
      yield SellerLoading();
      try {
        var sellers = await _sellerApi.getsellerInfoList();

        var bestsellers= sellers
            .where((element) => element.status == 'goodsellers')
            .toList();
           
        var badsellers= sellers
            .where((element) => element.status == 'badsellers')
            .toList();


        yield SellerInfoLoaded (
            badsellers: badsellers,
            bestsellers:bestsellers ,);
      } catch (e) {
        throw Exception();
      }
    }

Upvotes: 1

Related Questions