Repository navigation
Discuss MediaQuery.of in ui.window documentation #11697
Description
Activity
This is expected (sometimes by the time the Dart code runs we haven't yet heard from the OS what size the window should be). It happens more in faster builds (e.g. in release mode). Apps are expected to be able to handle being laid out at 0x0.
Reacted by MaryOk thanks for the quick response. I will follow up with a question if I may (if something like gitter or stack overflow is a better place please let me know): Are there any guidelines, notes, or documents what apps can do to handle zero size? Especially in case, like I discovered it originally, there is a CustomPainter paint(canvas, size) being passed zero size. canvas is not supposed to be painted beyond size, so the only solution is to return and paint nothing. Ok, but then it will remain so until CustomPainter.paint() is called again (or it the example above the State.build() is called again) - what would be the mechanism for the app to know when the window size is available, and how CustomPainter.paint() be caled again. Thanks
It should get called automatically when the new size is reported.
In the sample app above, if you use
MediaQuery.of(context)instead of reading straight offui.window, you should find your app is rebuilt automatically when the size changes.We've seen this come up before. I wonder if this is a foot-gun that we should somehow change the default behavior of MaterialApp to avoid? Something that folks diving down to the lower layers of our framework can get exposed to, but that we make easy to avoid doing the default thing?
Which part?
@Hixie Thanks for the
MediaQuery.of(context)suggestion (Ironically, I have used this in an earlier experiment but thought theui.window.physicalSizeis simpler and I can use it without context. Bad idea apparently).Appreciate the super quick responses on the weekend, feel free to close this (or let me know if I should).
Thanks to both, (also for an amazing framework)
@mzimmerm Did you look at any of the documentation when switching from
MediaQuery.oftoui.window? I'm curious if there's anywhere in the docs we could describe the trade-offs such that you would have seen it.- addedd: api docsIssues with https://api.flutter.dev/Issues with https://api.flutter.dev/
on Aug 20, 2017 - changed the title
[-] ui.window.physicalSize inside build() reported incorrectly as (0,0) on first run[/-][+]Discuss MediaQuery.of in ui.window documentation[/+]on Aug 20, 2017 @Hixie I did look at the docs, although I cannot claim the docs are misleading in any way. I pretty much live and try to find my info in
https://docs.flutter.io/index.html
Well, to give details: I am converting a simple charting library from Android to Flutter, and started looking at what in Flutter replaces
Android scale :
context.getResources().getDisplayMetrics().density;
one way or another, I found this (still in my code)final widgets.MediaQueryData mediaQueryData = widgets.MediaQuery.of(context); double scale = mediaQueryData.devicePixelRatio;
Then at some point I was looking at the docs of Window, and found:
https://docs.flutter.io/flutter/dart-ui/Window/physicalSize.html
and thought "ok I can use this instead", this is simple - the text about WidgetsBindingObserver I probably ignored as I did not 'get' it.
If anything, I'd probably add to
https://docs.flutter.io/flutter/dart-ui/Window/physicalSize.html
something about using
widgets.MediaQuery.of(context);instead with a bit of explanation. Perhaps there are other Window properties that have a similar problem.Sorry for the long comment, wanted to give context.
Fantastic, thanks. That's exactly what we needed to know! We'll add some docs there.
Happy to help in a small way. The Flutter team is doing amazing work, imho.
- addedframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.
on Dec 3, 2018 32 remaining items
- added a commit that references this issue
on Jul 20, 2023 - addedr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionand removedwaiting for PR to land (fixed)A fix is in flightA fix is in flight
on Jul 20, 2023 @Hixie Thanks for making this more clear to others. 👍
- added a commit that references this issue
on Jul 31, 2023 This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.- locked as resolved and limited conversation to collaborators
on Aug 3, 2023
Steps to Reproduce
Note: Terminate the app and Run (>) again a few times if first run does not reproduce.
This does not happen in 100% of tries; in the emulator, almost always. On a physical device (tried Z160) less often.
Logs
Logs from IDE
On first application start, the size is reported incorrectly as (0,0) - both in emulation and physical device.
After clicking the + Button, size reported correctly
Also correct after restarting (hitting the Run> button), size reported correctly
Logs from flutter run
Run
flutter analyzeand attach any output of that command also.Flutter Doctor
Paste the output of running
flutter doctorhere.The code
I can probably simplify, the only thing that seems to matter is asking for