没有上下文

发布于 2025-02-10 07:28:38 字数 1244 浏览 1 评论 0原文

我正在开发一个实时的患者监视应用程序,并且正在使用SignalR软件包处理插座。我有一些屏幕,一个用于套接字管理的飞镖文件,还有一个用于数据。在启动中,我打开插座并从中接收新数据。问题是当我想更新患者屏幕的状态并且行不通时。

这是用changeNotifierProvider部分包装父部件的一部分:

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,

这是套接字中的代码。在新数据到达时被调用的代码:

    await connection.start();

connection.on('UpdateOnlineGadgets', (updates) {
  DataClass().updateOnlineGadgets(updates![0].toString());
});

这是Dataclass中的代码,它是ChangeNotifier的扩展:

class DataClass with ChangeNotifier {
String _onlineGadgets = ' ';

String get onlineGadgets => _onlineGadgets;

void updateOnlineGadgets(String newGadgetsList) {
_onlineGadgets = newGadgetsList;
notifyListeners();
}
}

最后,这是用法在线游戏变量:

Text(Provider.of<DataClass>(context).onlineGadgets)

在套接字类中,我无法使用提供商访问Dataclass属性和方法。 (上下文),因为此类不是小部件,也没有上下文。我试图用该类的对象访问它,但似乎不起作用。我有什么选择?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文