繁体   English   中英

如何在 GetX Flutter 中获取 BuildContext?

[英]How can I get BuildContext in GetX Flutter?

class UpdateCheckerService extends GetxService{

  final newVersion = NewVersion(
    androidId: 'net.uniqueapps.beautyone',
  );

  UpdateCheckerService() {
    checkUpdate(newVersion);
  }

  void checkUpdate(NewVersion newVersion) {
    newVersion.showAlertIfNecessary(context: Get.context!); //how can I get context here
  }

}

我正在尝试使用 GetX 在 Flutter 中创建一个更新检查器,但是新版本插件询问上下文我给了 Get.context 但它抛出了 null。

获取上下文! Get.context 作为 BuildContext

 Get.context // this is BuildContext?
Get.context! // this is BuildContext
Get.context // this is BuildContext?
Get.context! // this is BuildContext

或者你可以使用这种方式

 Get.context as BuildContext // this is BuildContext

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM