MediaQuery.of(context).size的宽度和高度为零。

huangapple go评论129阅读模式
英文:

Width & Height of MediaQuery.of(context).size are zero

问题

有没有人在Flutter 3.10.1、Dart 3.0.1中遇到相同的问题?

MediaQuery.of(context).size.width

MediaQuery.of(context).size.height

始终为0。

MediaQuery.of(context).size.width

MediaQuery.of(context).size.height

在之前的Flutter 2.19中没有问题,但在升级后出现了此问题。

英文:

is there anyone facing the same issue in flutter 3.10.1, Dart 3.0.1?
The

MediaQuery.of(context).size.width 

and

MediaQuery.of(context).size.height 

are always 0.

MediaQuery.of(context).size.width 

and

MediaQuery.of(context).size.height 

had worked with no issue in previous Flutter 2.19, but i get this issue after upgrade.

答案1

得分: 1

我遇到了相同的问题 - MediaQuery.of(context).size.width 返回零,它在Flutter 3.7.12中工作,但在3.10.5中停止工作。
在我的情况下,这是因为我的一个小部件被包裹在MediaQuery中:

  return MediaQuery(
    data: const MediaQueryData(),
    child: MaterialApp(home: home),
  );

我只是移除了这个MediaQuery,然后它就正常工作了。

英文:

I have the same issue - MediaQuery.of(context).size.width return zero and it works in Flutter 3.7.12 but stop works in 3.10.5.
In my case it happens cause one of my widgets was wrapped with MediaQuery:

  return MediaQuery(
    data: const MediaQueryData(),
    child: MaterialApp(home: home),
  );

I just removed this MediaQuery and it works

答案2

得分: 0

我正在这样调用它:

  @override
  Widget build(BuildContext context) {
   
    Size screenSize = MediaQuery.of(context).size;
    dev.log('宽度: ${screenSize.width}');
    dev.log('高度: ${screenSize.height}');
    // ....
  }

而且这对我来说运行得非常顺利!我始终使用最新的稳定更新。

我还尝试在一个非常小的Container中调用它,即使在那种情况下,我仍然可以获得全屏的大小。

  • 自从您更新以来,有一些其他部分不正常工作,因此您的尺寸测量没有正确触发。
  • 您需要清理项目,例如:1)运行 flutter clean,2)运行 flutter pub get,3)关闭并重新打开您的编辑器(如果需要,重复1和2)。
  • 您是否调用它不正确?

(较不可能的情况):

  • 您是否正在使用真实设备并且某种方式被阻止了?
  • 您的模拟器是否在阻止它或者出现了“损坏”?
英文:

I am calling it like

  @override
  Widget build(BuildContext context) {
   
    Size screenSize = MediaQuery.of(context).size;
    dev.log('Width:  ${screenSize.width}');
    dev.log('Heigth:  ${screenSize.height}');
....}

And this is working flawless for me! I have always the newest stable update.

I also tried to call it in a really small Container, also then I still get the full size of the screen.

  • since you updated, there are some other parts not working fine, so you size measurement doesnt fire correctly
  • You have to clean your project like: 1) flutter clean, 2) flutter pub get 3) close and reopen your editor. (If you want repeat 1 and 2).
  • You calling it wrong?

(Less Likely):

  • you are using a real device and it is blocking it some how?
  • your emulator is blocking it or its "broken"?

答案3

得分: 0

我使用GetX和Sizer包装在MaterialApp()上。是的,这也发生在我身上。不仅如此,您的runApp()有时甚至void main()也会运行多次。我认为这个问题来自Flutter的一面。但如果在您的情况下只有runApp()运行多次,那么目前我通过以下方式解决了它...

if (MediaQuery.of(context).size.width > 0) return MaterialApp();
return const SizedBox();

您需要使用builder或StatelessWidget来获取上下文。如果您在项目设置中找到更好的解决方案,请分享它们。

英文:

I use Getx and Sizer wrapping over MaterialApp (). Yes that happens with me too. Not only that, your runApp() and sometimes even void main() also gets run multiple times. I think that problem is from flutter side. But if in your case only runApp() gets run multiple times then, currently I solved it by ...

if(MediaQuery.of(context).size.width > 0) return MaterialApp();
return const SizedBox();

you need to use builder or StatelessWidget for context. If you find any better solution from project setting, please share them.

huangapple
  • 本文由 发表于 2023年5月25日 15:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定