You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MediaQuery.of(context).size.height returns 0 on startup in release mode #25827
When I log MediaQuery.of(context).size.height in MyWidget's build() function, I get the following output in release mode:
12-27 18:43:39.650 12694 12740 I flutter : Context height: 0.0
12-27 18:43:39.854 12694 12740 I flutter : Context height: 731.4285714285714
The behavior is as expected in debug mode (height is never 0.0). I am doing some calculations with this
value that would result in some widgets having infinite height, which causes the application to freeze on startup.
That's because Flutter starts quicker in release mode and the native platform reports the actual resolution with a delay.
Just handle the case that the size is 0/0 at first.
@gordon-rawe there shouldn't be much to solve.
Just make your code resilient against 0 values so that it doesn't cause an error.
Shortly afterwards your app will be rebuilt again with the proper value.
Ran into the same problem. I was debugging for hours until I found out that size was empty. There was no hot-reload in release mode so it made debugging much more difficult.
Basically, you need to switch out to use FutureBuilder and use a stream with a future to watch and capture the value once it's not zero.
Here is an example function to capture to streams value into a future:
Future<double> whenNotZero(Stream<double> source) async {
awaitfor (double value in source) {
print("Width:"+ value.toString());
if (value >0) {
print("Width > 0: "+ value.toString());
return value;
}
}
// stream exited without a true value, maybe return an exception.
}
Then in your widgets you specify the furture builder. In this example I an checking every 50 milliseconds to MediaQuery.of(context).size.width to see if its greater than 0.
any updates? it is big problem because in build we have different layouts, which we select according sizes, you should guaranties that in build we have valid context and in didChangeDependencies as well.
That's because Flutter starts quicker in release mode and the native platform reports the actual resolution with a delay.
Just handle the case that the size is 0/0 at first.
I've encountered this problem yesterday. It never happened on emulator. But then i tested my app on real device and MediaQuery.of(context).size.width returned 0. The UI of course didn't work properly because of this. When i "hot restarted" the app, it returned proper width. I also tried to stop the app and restarted it.., but it didn't happen again. It happens only every now and then. So it's kind of a hidden "bug". I think, this should be resolved somehow or at least some official docs how to handle this issue? I have now implemented the solution provided by @joseph-montanez ,but i am not sure if this is the best possible approach? Isn't there something more "clean"? Is anyone even aware of this problem? I think it should be marked as a bug.
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 -v and a minimal reproduction of the issue.
Activity
zoechi commentedon Dec 28, 2018
That's because Flutter starts quicker in release mode and the native platform reports the actual resolution with a delay.
Just handle the case that the size is
0/0at first.HansMuller commentedon Jan 2, 2019
Related problem with TabBar: #23339 (comment)
gordon-rawe commentedon Jan 3, 2019
did you solved this problem?
zoechi commentedon Jan 3, 2019
@gordon-rawe there shouldn't be much to solve.
Just make your code resilient against
0values so that it doesn't cause an error.Shortly afterwards your app will be rebuilt again with the proper value.
dsrenesanse commentedon Feb 24, 2019
looks like a crutch
leen-nobbas commentedon Mar 21, 2019
That is not a good thing!
lianhuilui commentedon Mar 24, 2019
Ran into the same problem. I was debugging for hours until I found out that size was empty. There was no hot-reload in release mode so it made debugging much more difficult.
HipSze commentedon Mar 25, 2019
same problem, I just release my app on store.
joseph-montanez commentedon Mar 30, 2019
I've created a video and quick gist on this issue with a solution.
Video https://www.youtube.com/watch?v=zp26UXInb4s&feature=youtu.be
Gist https://gist.github.com/joseph-montanez/c7316b89c9a458a34c6381840cc6d1de
Basically, you need to switch out to use
FutureBuilderand use a stream with a future to watch and capture the value once it's not zero.Here is an example function to capture to streams value into a future:
Then in your widgets you specify the furture builder. In this example I an checking every 50 milliseconds to
MediaQuery.of(context).size.widthto see if its greater than 0.@zoechi hopefully this will be a better answers, as there are multiple reports of this issue.
jefferybai commentedon Apr 26, 2019
same problem, I just release my app on store.
Finally fixed this hell of a bug that fricking fked me up for days
jlab13 commentedon Sep 19, 2019
same problem, I just release my app on store.
topilski commentedon Nov 28, 2019
any updates? it is big problem because in build we have different layouts, which we select according sizes, you should guaranties that in build we have valid context and in didChangeDependencies as well.
JSBmanD commentedon Dec 26, 2019
Anyone solved the issue?
kf6gpe commentedon Jan 7, 2020
This is by design for performance reasons, as @zoechi mentions. I'm going to close this issue with the ways to work with this currently listed here.
szbk commentedon Jan 13, 2020
I had a similar problem. I solved it like this;
sciobotaru commentedon Sep 3, 2020
Had the exact same issue. I have changed the code as follow, then everything worked fine in release mode:
@OverRide
Widget build(BuildContext context) {
InitialData.phoneSize = MediaQuery
.of(context)
.size;
}
sciobotaru commentedon Sep 3, 2020
Thanks, man! Your comment really helped me.
teneon commentedon Feb 13, 2021
I've encountered this problem yesterday. It never happened on emulator. But then i tested my app on real device and MediaQuery.of(context).size.width returned 0. The UI of course didn't work properly because of this. When i "hot restarted" the app, it returned proper width. I also tried to stop the app and restarted it.., but it didn't happen again. It happens only every now and then. So it's kind of a hidden "bug". I think, this should be resolved somehow or at least some official docs how to handle this issue? I have now implemented the solution provided by @joseph-montanez ,but i am not sure if this is the best possible approach? Isn't there something more "clean"? Is anyone even aware of this problem? I think it should be marked as a bug.
tjx666 commentedon Mar 30, 2021
@teneon
This seems to be more simple refer to https://github.com/OpenFlutter/flutter_screenutil/blob/20ac1de0f2f2b95d43922d1567fa2aa1629c8cee/lib/screenutil_init.dart#L22.
github-actions commentedon Aug 3, 2021
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.