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:
Release Output:
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:
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