Senthur Kumaran
Senthur Kumaran

Reputation: 1331

Get.height and Get.width are printing 0.0 on first time in release mode?

I am using Getx state management for my project. Get.width and Get.height is printing 0.0 when the widget build for the first time after refreshing the screen with setState((){}) it prints the actual value. I have declared and initialized the height and width commonly.

This is my code:

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      enableLog: true,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    print(Get.height);
    print(Get.width);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Debug Output:

DeBug  Image

Release Output:

enter image description here

First, it prints the value 0.0 after pressing the floating button it started to print the actual value.

How can I solve this issue? In my previous projects, it was working fine.

get: ^4.6.5

My flutter Doctor:

enter image description here

Upvotes: 0

Views: 2674

Answers (3)

mkiziltay
mkiziltay

Reputation: 63

You can use a splash screen and you can call dimensions class with getx while splash screen display. Because size not be null or 0.0 if splash screen display currently. call class after display not before display.

Or you may use didmetrics changed for listen dimensions changing and then refresh layout.

Or you may read my medium story (Fluter UI error after release, release mode render bugs.) you can find my medium profile link on my stackoverflow profile/website section.

Upvotes: 0

Senthur Kumaran
Senthur Kumaran

Reputation: 1331

In the splash screen I have used the MediaQuery.of(context).size.width and MediaQuery.of(context).size.height for height and width. Then in the other pages I have used the Get.width and Get.height.Now everything works fine. If there is no height and width property in the Splash screen() then use the MediaQuery.of(context).size.width and MediaQuery.of(context).size.height in the login page and the home page. Hope this solve the issue.

Upvotes: 1

mohammad esmaili
mohammad esmaili

Reputation: 1747

according to issues #2523 on github there is no way to fix for now, but you can try to use:

MediaQuery.of(context).size.width
MediaQuery.of(context).size.height

more info about Get and MediaQuery

but this error maybe happens when the UI has not been sized and laid out on the screen yet

Upvotes: 1

Related Questions