0
votes

so I'm currently working on a project using flutter and BloC pattern to handle state, but I'm struggling to find the getBloc method so if there's anyone who can help me, I will be very thankful.

itemContainerClass:

class ItemContainer extends StatelessWidget {
  final FoodItem foodItem;

  ItemContainer(@required this.foodItem);


  final CartListBloc bloc=CartProvider.getBloc<CartListBloc>();

  addToCart(FoodItem fooditem){
    bloc.addToList(foodItem);
  }

cartListBloc class:

    import 'dart:async';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:rxdart/rxdart.dart';

import 'package:fooddelivery/Food/foodItem.dart';
import 'provider.dart';

class CartListBloc extends BlocBase {
  CartListBloc();

//Stream that receives a number and changes the count;

  CartProvider _cartProvider = CartProvider();
  final _cartItemController= BehaviorSubject<List<FoodItem>>.seeded([]);

//output
  Stream<List<FoodItem>> get listStream => _cartItemController.stream;

  Sink<List<FoodItem>> get listSink => _cartItemController.sink;



//Business logic
  addToList(FoodItem _foodItem) {
    _cartItemController.sink.add(_cartProvider.addToList(_foodItem));
  }

  removeFromList(FoodItem _foodItem) {
    _cartItemController.sink.add(_cartProvider.removeFromList(_foodItem));
  }

//dispose will be called automatically by closing its streams
  @override
  void dispose() {
    _cartItemController.close();
    super.dispose();
  }
}

cart provider class:

import 'package:flutter/material.dart';
import 'package:fooddelivery/BLoC/cartListBloc.dart';

import 'package:fooddelivery/Food/foodItem.dart';


class CartProvider{
  List<FoodItem> _foodItems=[];


  List<FoodItem> addToList(FoodItem _foodItem){
    _foodItems.add(_foodItem);
    return _foodItems;
  }

  List<FoodItem> removeFromList(FoodItem _foodItem){
    _foodItems.remove(_foodItem);
    return _foodItems;
  }

}

I'm trying to follow some tutorial on the web but I didn't solve it. I'm here for any clarification or more details.

1
I'm highly discourage the use of Bloc pattern, it's dirt and kinda unclear, use Provider architecture instead. Provider is the closer you could get to an mvvm pattern, also check this video that mix provider with proxy :D - FabriBertani
Thank u bro, can u tell me the difference between them ? - khaliloos benk

1 Answers

0
votes

I know this post is old but just in case someone sees this.

First of all why do you use CartProvider.getBloc to get the bloc? Since you have defined CartProvider inside the bloc so it is better you just declare the bloc like this:

final bloc = CartListBloc();

Then secondly the events you declared is not very clear. Seems that you wanna be able to add and remove item from the list right?

So the adding of events should be called in UI level whenever a user click a button (on an onTap callback for example)

onTap: () => bloc.addToList(newItem)

But if it is just a list you probably do not need a Bloc. Because Bloc is for managing Stream of data that can come at anytime. Or managing states depending on the Stream of data Or perhaps you have other use cases?

Anyway, for Blocs with Flutter I highly recommend flutter_bloc package (https://pub.dev/packages/flutter_bloc) and implement all the states and events classes altogether. Define firstly your events and states then implement your logic components.

Or for a simpler version of Bloc there is a good tutorial on this can be found here: https://www.youtube.com/watch?v=Un7eG5hHNPg&list=PLRAV69dS1uWT_JKom7xDKdHl1gp3o_AH1

I hope this helps and not making you confused.