简体   繁体   中英

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.

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();
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM