getx icon indicating copy to clipboard operation
getx copied to clipboard

Get.height and Get.width return 0.0 on Android platform using profile and release build methods

Open Thiago-LM opened this issue 2 years ago • 8 comments

Describe the bug On Android platform, using the profile and release build methods, the getters Get.size, Get.mediaQuery.size, Get.height and Get.width fail to get the correct values the first time they are called (both come as 0.0). Calling MediaQuery.of(context).size instead, the first time you need it, manages to make them get the correct value afterwards.

Reproduction code

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() => runApp(const GetMaterialApp(home: MyHomePage()));

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    print('Get.size = ${Get.size}');
    print('Get.mediaQuery.size = ${Get.mediaQuery.size}');
    print('Get.height = ${Get.height} / Get.width = ${Get.width}');
    print('Get.size.height = ${Get.size.height} / Get.size.width = ${Get.size.width}');
    print('Get.mediaQuery.size.height = ${Get.mediaQuery.size.height} / Get.mediaQuery.size.width = ${Get.mediaQuery.size.width}');
    return Get.height != 0.0
        ? Scaffold(
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const <Widget>[Text('Example text.')],
              ),
            ),
          )
        : Container();
  }
}

To Reproduce Steps to reproduce the behavior:

  1. Create a Flutter project with flutter create project_name
  2. Add get: ^4.6.5 to your pubspec.yaml file (can be done with flutter pub add get)
  3. Paste the example above on the main.dart file
  4. Build profile or release methods on Android platform (flutter run --profile or flutter run --release)

Expected behavior App should open normally, showing a blue AppBar and a white body with "Example text." written in black at the middle of the screen.

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...                              9.2s
✓  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app-debug.apk...          807ms
Syncing files to device sdk gphone64 x86 64...                      97ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

💪 Running with sound null safety 💪

An Observatory debugger and profiler on sdk gphone64 x86 64 is available at: http://127.0.0.1:60573/w7wmzT49w_E=/
I/flutter ( 8482): Get.size = Size(392.7, 781.1)
I/flutter ( 8482): Get.mediaQuery.size = Size(392.7, 781.1)
I/flutter ( 8482): Get.height = 781.0909090909091 / Get.width = 392.72727272727275
I/flutter ( 8482): Get.size.height = 781.0909090909091 / Get.size.width = 392.72727272727275
I/flutter ( 8482): Get.mediaQuery.size.height = 781.0909090909091 / Get.mediaQuery.size.width = 392.72727272727275
The Flutter DevTools debugger and profiler on sdk gphone64 x86 64 is available at: http://127.0.0.1:9102?uri=http://127.0.0.1:60573/w7wmzT49w_E=/

Found behavior App opens to a black screen.

Launching lib\main.dart on sdk gphone64 x86 64 in release mode...
Running Gradle task 'assembleRelease'...                           25.0s
✓  Built build\app\outputs\flutter-apk\app-release.apk (6.7MB).
Installing build\app\outputs\flutter-apk\app-release.apk...        403ms

Flutter run key commands.
h List all available interactive commands.
c Clear the screen
q Quit (terminate the application on the device).
I/flutter ( 7547): Get.size = Size(0.0, 0.0)
I/flutter ( 7547): Get.mediaQuery.size = Size(0.0, 0.0)
I/flutter ( 7547): Get.height = 0.0 / Get.width = 0.0
I/flutter ( 7547): Get.size.height = 0.0 / Get.size.width = 0.0
I/flutter ( 7547): Get.mediaQuery.size.height = 0.0 / Get.mediaQuery.size.width = 0.0

Screenshots

Expected Behavior Found Behavior
Screenshot 2022-08-28 231819 Screenshot 2022-08-28 231945

Flutter Version:

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.7.7, on Microsoft Windows [Version 10.0.22621.1413], locale en-US)
[✓] Windows Version (Installed version of Windows is version 10 or higher)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Chrome - develop for the web
[✓] Visual Studio - develop for Windows (Visual Studio Community 2022 17.5.0)
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.76.2)
[✓] Connected device (4 available)
[✓] HTTP Host Availability

• No issues found!

Getx Version: get: ^4.6.5

Describe on which device you found the bug: Samsung Galaxy M12 - Android 11 Xiaomi Mi A3 - Android 11 Xiaomi Poco X3 NFC - Android 12 Xiaomi Poco F1 - Android 10

Thiago-LM avatar Aug 29 '22 02:08 Thiago-LM

Had the same issue, and spent a whole day debugging why my widgets are not showing up correctly. Flutter: 3.3.0 GetX: 4.6.5

sukhcha-in avatar Sep 02 '22 17:09 sukhcha-in

Same here...

Get.context looks to be null...

prooshani avatar Sep 08 '22 22:09 prooshani

Been stuck in this for almost a week, thinking that it was a Flutter internal problem. Flutter: 3.3.0 Getx: 4.6.5 For now, I'm using MediaQuery.of(context).size and it's working as expected.

vitorpedeo avatar Oct 08 '22 10:10 vitorpedeo

Same here, using the debug version works just as normal

Gustavisk13 avatar Oct 08 '22 11:10 Gustavisk13

Same here, when the widget was built for the first time it was giving 0.0. but after refreshing the screen with setState((){}) it gives the actual value.

https://stackoverflow.com/questions/74002358/get-height-and-get-width-are-printing-0-0-on-first-time-in-release-mode/74002458#74002458

Senthuran-01 avatar Oct 09 '22 06:10 Senthuran-01

I have solved this issue by using MediaQuery.of(context).size.width and MediaQuery.of(context).size.height in the first screen that appear when the app runs. For the other pages i have used the Get.width and Get.height.

Senthuran-01 avatar Oct 17 '22 06:10 Senthuran-01

I encountered a similar issue where my app worked fine in debug mode, but I encountered a white screen problem in release mode. After spending two days searching for the cause, I discovered that the issue stemmed from my usage of Get.width and Get.height. The problem was resolved by replacing them with MediaQuery.of(context).size.width or MediaQuery.of(context).size.height.

muhammad6535 avatar Jul 02 '23 11:07 muhammad6535

I was getting the same error, while we were calling Get.size in the initState() function of the main widget.

Calling this Get.size in the " WidgetsBinding.instance.addPostFrameCallback((_) => afterInitState());" solved the problem. I called Get.size() in "afterInitState()" function

bbright-dinesh avatar Dec 19 '23 13:12 bbright-dinesh