Flutter BLoC and Provider: A Shopping Cart Example - Cart Bloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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