0

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?

  • Could you please give more information about the place you want to use the data? And why it is not related to any widget? – Lebecca Jun 25 '22 at 16:34
  • And also, [this](https://stackoverflow.com/questions/57415617/how-to-access-to-provider-field-from-class-that-do-not-have-context) may solve your problem. – Lebecca Jun 25 '22 at 16:36
  • I am using the data in a simple Text widget in the patient screen which is a StatefulWidget. I also saw that answer but I'm kinda new to flutter and I couldn't understand, how to use it in my case. – Kiumars Javan Jun 25 '22 at 17:12

0 Answers0