I am working on a real-time patient monitoring app and I am using the signalR package for dealing with sockets. I have some screens, a dart file for socket management, and one for data. in the startup, I open the socket and receive new data from it. the problem is when I want to update the state of the patient's screen and it doesn't work.
this is the wrapping of the parent widget with ChangeNotifierProvider part:
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
MySocket theSocket = MySocket();
theSocket.openSocket();
return ChangeNotifierProvider(
create: (_) => DataClass(),
lazy: false,
//builder: (context, _) => MyApp(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
this is the code in the socket.dart that gets invoked when new data arrives:
await connection.start();
connection.on('UpdateOnlineGadgets', (updates) {
DataClass().updateOnlineGadgets(updates![0].toString());
});
this is the code in the DataClass which is an extends of ChangeNotifier:
class DataClass with ChangeNotifier {
String _onlineGadgets = ' ';
String get onlineGadgets => _onlineGadgets;
void updateOnlineGadgets(String newGadgetsList) {
_onlineGadgets = newGadgetsList;
notifyListeners();
}
}
and finally, this is the usage of the onlineGadgets variable:
Text(Provider.of<DataClass>(context).onlineGadgets)
in the socket class, I can't access DataClass properties and methods with Provider. of(context) because this class is not a widget and doesn't have context. I tried to access it with an object of that class but it seems to not work. what are my options?