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