Created
April 3, 2020 14:47
-
-
Save VB10/caad8e1dbc2a5161545ae689c606d10f to your computer and use it in GitHub Desktop.
Shoppi - Shop VM
This file contains hidden or 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
| class ShopViewModel extends BaseViewModel with ChangeNotifier { | |
| // IO.Socket socket = IO.io(AppConstants.SOCKET_URL); | |
| IO.Socket socket = IO.io(AppConstants.SOCKET_URL, <String, dynamic>{ | |
| 'transports': ['websocket'], | |
| // 'extraHeaders': {'foo': 'bar'} // optional | |
| }); | |
| ProductService service = ProductService(); | |
| List<Product> subList = []; | |
| List<Product> productList = []; | |
| Future<List<Product>> getAllProduct() async { | |
| final list = await service.fetchProducts(); | |
| productList = list; | |
| return list; | |
| } | |
| @override | |
| void setContext(BuildContext context) { | |
| this.context = context; | |
| initSocket(); | |
| } | |
| void initSocket() { | |
| socket.connect().on(AppConstants.SOCKET_CHANNEL_DELIVERY, (data) { | |
| // var jsonModel = jsonDecode(data); | |
| var model = Product.fromJson(data); | |
| print(model); | |
| final index = | |
| productList.indexWhere((element) => element.sId == model.sId); | |
| if (index != -1) { | |
| productList[index] = model; | |
| } | |
| notifyListeners(); | |
| }); | |
| } | |
| void sendMessage() { | |
| socket.emit('chat message', 'test'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment