简体   繁体   English

Flutter Get.width 或初始化 MediaQuery.of(context).size 在 Release 模式下不起作用

[英]Flutter Get.width or initialize MediaQuery.of(context).size not working in Release mode

When ı use get.width or height at first page in project or initialize like this ;当我在项目的第一页使用get.width或height或像这样初始化时;

 @override
  void didChangeDependencies() {
    SizeConfig().init(context);
    super.didChangeDependencies();
  }

also SizeConfig page :还有 SizeConfig 页面:

class SizeConfig {
  static late MediaQueryData _mediaQueryData;
  static late double width;
  static late double height;
  static late double blockSizeHorizontal;
  static late double blockSizeVertical;
  static late double res;

  void init(BuildContext context) {
    _mediaQueryData = MediaQuery.of(context);
    width = _mediaQueryData.size.width;
    height = _mediaQueryData.size.height;
  }

} }

It is not working.它不工作。

But when ı use debug mode they are working well.但是当我使用调试模式时,它们运行良好。 Yes the release mode faster than debug mode but how can we solve it?是的,发布模式比调试模式快,但我们该如何解决呢?

Try below code试试下面的代码

void init(BuildContext context) {
    WidgetsBinding.instance!.addPostFrameCallback((_) {
  // executes after build
_mediaQueryData = MediaQuery.of(context);
    width = _mediaQueryData.size.width;
    height = _mediaQueryData.size.height;

});
  }

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

相关问题 Flutter MediaQuery.of(context).size.width 检查设备大小总是在 iOS 模拟器中返回相同的错误值 - Flutter MediaQuery.of(context).size.width to check device-size always returns same wrong value in the iOS Simulator MediaQuery.of(context) 在 Flutter 应用程序中导致错误 - MediaQuery.of(context) causing error in Flutter app 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error 在 flutter 中使用 MediaQuery 的高度和宽度大小时出错 - Error while using MediaQuery for size of height and width in flutter Android-使用get.Width()时应用崩溃 - Android - app crashes when using get.Width() 当我将 MediaQuery.of(context).padding.top 与 appBar 一起使用时,它返回 0.0。 这是为什么? - MediaQuery.of(context).padding.top is returning 0.0 when I'm using it with appBar. Why is that? 未检测到键盘。 MediaQuery.of(context).viewInsets.bottom 总是返回 0.0 - Keyboard not being detected. MediaQuery.of(context).viewInsets.bottom always returns 0.0 MediaQuery 无法获取宽度和高度 - MediaQuery fail to get width and height 为什么 Flutter blue plus 在发布模式下不起作用? - Why Flutter blue plus is not working on release mode? flutter_background_geolocation 在发布模式下不工作 - flutter_background_geolocation not working in release mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM