Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flutter BLoC and Provider: A Shopping Cart Example - Cart Bloc
import 'package:flutter/material.dart';
class CartBloc with ChangeNotifier {
Map<int, int> _cart = {};
Map<int, int> get cart => _cart;
void addToCart(index) {
if (_cart.containsKey(index)) {
_cart[index] += 1;
} else {
_cart[index] = 1;
}
notifyListeners();
}
void clear(index) {
if (_cart.containsKey(index)) {
_cart.remove(index);
notifyListeners();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.