Skip to content

Instantly share code, notes, and snippets.

@VB10
Created April 3, 2020 14:47
Show Gist options
  • Save VB10/caad8e1dbc2a5161545ae689c606d10f to your computer and use it in GitHub Desktop.
Save VB10/caad8e1dbc2a5161545ae689c606d10f to your computer and use it in GitHub Desktop.
Shoppi - Shop VM
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