提供者:如何将B的听众通知听众

发布于 2025-02-05 03:24:57 字数 1801 浏览 2 评论 0原文

我有两个使用ChangeNotifier的控制器在我的应用程序中使用。

方法1

(控制器A):

class DrawController extends ChangeNotifier {

   void clearCanvas() {
   selectedText = '';
   shouldClearCanvas = true;
   currentDrawPoints.initialPoint = null;
   currentDrawPoints.endPoint = null;
   print(currentDrawPoints.initialPoint);
   notifyListeners();
   }
}

从(控制器B)调用它:

class GameController extends ChangeNotifier {

  DrawController drawController = DrawController();

  void someFunction(){

     drawController.clearCanvas();
     notifyListeners();

   }

}

上面的方法不起作用。它确实执行功能并更改值,但不会更改UI(不重建小部件)。

方法2:

(控制器A):

class DrawController extends ChangeNotifier {

   void clearCanvas() {
   selectedText = '';
   shouldClearCanvas = true;
   currentDrawPoints.initialPoint = null;
   currentDrawPoints.endPoint = null;
   print(currentDrawPoints.initialPoint);
   notifyListeners();
   }
}

从(控制器B)调用它:

class GameController extends ChangeNotifier {

  DrawController drawController = DrawController();

  void someFunction(BuildContext context){

     Provider.of<DrawController>(context, listen: false).clearCanvas();

   }

}

第二种方法也可以更新UI,但我认为这是错误的方法。

正如在异步等待之后使用它一样,警告“请勿在异步差距上使用buildContext”。

  class GameController extends ChangeNotifier {

  DrawController drawController = DrawController();

  void someFunction(BuildContext context) async {

     User? userData = await localStorage.getUserData();   

     Provider.of<DrawController>(context, listen: false)
      .clearCanvas(); //Gives warning "Do not use BuildContexts across async gaps."

   }

}

谁能告诉我正确的方法。

I have two controllers using the changenotifiers in my app and I want to notify listeners of A from B. I have be trying two different methods for the same.

Method 1

(Controller A):

class DrawController extends ChangeNotifier {

   void clearCanvas() {
   selectedText = '';
   shouldClearCanvas = true;
   currentDrawPoints.initialPoint = null;
   currentDrawPoints.endPoint = null;
   print(currentDrawPoints.initialPoint);
   notifyListeners();
   }
}

Calling it from (Controller B):

class GameController extends ChangeNotifier {

  DrawController drawController = DrawController();

  void someFunction(){

     drawController.clearCanvas();
     notifyListeners();

   }

}

The above method doesn't work. It does execute the function and changes the value but it does not change the UI (does not rebuild the widget).

Method 2:

(Controller A):

class DrawController extends ChangeNotifier {

   void clearCanvas() {
   selectedText = '';
   shouldClearCanvas = true;
   currentDrawPoints.initialPoint = null;
   currentDrawPoints.endPoint = null;
   print(currentDrawPoints.initialPoint);
   notifyListeners();
   }
}

Calling it from (Controller B):

class GameController extends ChangeNotifier {

  DrawController drawController = DrawController();

  void someFunction(BuildContext context){

     Provider.of<DrawController>(context, listen: false).clearCanvas();

   }

}

This second method works and it updates the UI too but I think this is the wrong method of doing it.

As using it after async await gives the warning "Do not use BuildContexts across async gaps."

  class GameController extends ChangeNotifier {

  DrawController drawController = DrawController();

  void someFunction(BuildContext context) async {

     User? userData = await localStorage.getUserData();   

     Provider.of<DrawController>(context, listen: false)
      .clearCanvas(); //Gives warning "Do not use BuildContexts across async gaps."

   }

}

Can anyone tell me the correct way of doing it.

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

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

发布评论

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