getx
getx copied to clipboard
Get.width returns null and causes exception in main.dart
Describe the bug Get.width returns null and causes exception in main.dart
**Reproduction code
.... necessary imports, including get
import 'package:get/get.dart';
void main([param0 = false]) async {
ThemeData? theme;
try {
print("$tag:1 ${DateTime.now()} Starting main param0: $param0");
WidgetsFlutterBinding.ensureInitialized();
print("$tag:2 ${DateTime.now()} Starting main param0: $param0");
print("$tag:3 ${DateTime.now()} Starting main width: ${Get.width} height: ${Get.height} $param0");
// other codes...
} catch (e) {
print_log("Exception in main: $e");
}
}
To Reproduce In above code, tag1 and tag2 lines displayed, then, exception occured. Same code was working previously. Now: working on linux but not on windows. Exactly same code works on linux, but not on windows. Apparently, something seems broken.
Expected behavior tag3 line also printed, program should continue.
Screenshots
A Dart VM Service on Chrome is available at: http://127.0.0.1:61000/J3abkndvzOs=
-main.dart:1 2023-09-13 01:30:18.577 Starting main param0: []
-main.dart:2 2023-09-13 01:30:18.717 Starting main param0: []
-Exception in main: Unexpected null value.
Flutter Version: flutter --version Flutter 3.13.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision 2524052335 (6 days ago) • 2023-09-06 14:32:31 -0700 Engine • revision b8d35810e9 Tools • Dart 3.1.1 • DevTools 2.25.0
Getx Version: 4.6.6
Describe on which device you found the bug: Web-Chrome (On windows only)
Minimal reproduce code as given above
Getting the Same issue, Tried below work around, I kept that code in side a Future Delayed.
Future.delayed(Duration.zero,() {
// put your logic using Get.width
},);
Tested with newer version flutter 3.13.4, same situation.
Experiencing the same issue. Anyone had any luck?
This error occurs because:
- You are trying to access
Get.width
outside the build method of a widget.
- In Flutter, some properties like MediaQuery (e.g., screen width) can only be reliably accessed during or after the build phase.
- You might be trying to access
Get.width
before it's available due to widget lifecycle considerations. In such cases, you should use it after the widget has been built, for example, in theinitState
method or a callback triggered by a layout event.
`import 'package:flutter/material.dart';
import 'package:get/get.dart';
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// You can safely use Get.width within the build method
double screenWidth = Get.width;
return Container(
width: screenWidth,
height: 100,
color: Colors.blue, );}}
I am using Get.width inside build method still getting the same error
return Image.network( widget.banners[index].getThumbnail(), fit: BoxFit.cover, height: 428, width: Get.width, // width: MediaQuery.of(context).size.width, );
From the docs:
Get.widget should not be used inside widgets or build methods. Use context.width instead of.
Get.widget should be used after build cycles (like in a button's onPressed) and input user actions.
Why? Because during the construction of the widget it is not available (or outdated), but later (when user input events are available) it has the updated value and is ready to use. If you are inside the widget tree, you have easy access to context, then context.widget will be the best option.