Skip to content

Instantly share code, notes, and snippets.

@junjizhi
Created July 13, 2019 21:19
Show Gist options
  • Save junjizhi/8f8c32b3ce8fed7495548ccea4d8063e to your computer and use it in GitHub Desktop.
Save junjizhi/8f8c32b3ce8fed7495548ccea4d8063e to your computer and use it in GitHub Desktop.
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