-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MediaQuery.of(context).size.height returns 0 on startup in release mode #25827
Comments
That's because Flutter starts quicker in release mode and the native platform reports the actual resolution with a delay. |
Related problem with TabBar: #23339 (comment) |
did you solved this problem? |
@gordon-rawe there shouldn't be much to solve. |
looks like a crutch |
That is not a good thing! |
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. |
same problem, I just release my app on store. |
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 Basically, you need to switch out to use Here is an example function to capture to streams value into a future: Future<double> whenNotZero(Stream<double> source) async {
await for (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 FutureBuilder(
future: whenNotZero(
Stream<double>.periodic(Duration(milliseconds: 50),
(x) => MediaQuery.of(context).size.width),
),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data > 0) {
return Container(
child: //... Your widgets here!
);
}
} else {
return Container();
}
}
), @zoechi hopefully this will be a better answers, as there are multiple reports of this issue. |
same problem, I just release my app on store. |
Fk this stupid bug. flutter/flutter#25827 (More about the bug: This was a bug where the CustomBottomSheet did not appear in release mode. The reason was due to the issue above. I took fking forever to solve this shit)
same problem, I just release my app on store. |
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. |
Anyone solved the issue? |
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. |
Had the exact same issue. I have changed the code as follow, then everything worked fine in release mode: @OverRide
} |
Thanks, man! Your comment really helped me. |
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. |
@teneon Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth != 0) {
return MaterialApp(
home: HomePage()
}
return Container();
},
);
} |
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 |
I have a widget which is a member of a stack that's in the body of Scaffold:
When I log
MediaQuery.of(context).size.height
in MyWidget's build() function, I get the following output in release mode: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.
The text was updated successfully, but these errors were encountered: