Flutter自定义SizeConfig类遭遇LateInitializationError及Null check错误的求助
Hey there! Let's work through fixing those frustrating LateInitializationError and Null check errors with your custom SizeConfig class.
First, let's break down why these issues are happening:
- The
LateInitializationErrorpops up because you markedscreenWidthandscreenHeightwithlate, but tried to use them before callingSizeConfig.init(context). Thelatekeyword guarantees the variable will be initialized before use, and Flutter throws an error when that promise is broken. - When you switched to nullable variables (
static double? screenWidth), the Null check error occurred because you accessed the nullable value directly without confirming it wasn't null—Flutter won't let you treat a nullable value as non-null without an explicit check.
Here are some simple, effective fixes:
1. Initialize SizeConfig early in your app
The core fix is to run SizeConfig.init(context) before any widget tries to use your proportionate size methods. You can do this in your app's root using a Builder widget (which gives you a valid BuildContext to access MediaQuery):
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Your Flutter App', home: Builder( builder: (context) { // Initialize SizeConfig here before navigating to your main page SizeConfig.init(context); return HomePage(); // Replace with your actual home widget }, ), ); } }
This ensures screenWidth and screenHeight are set before any widget calls your helper methods.
2. Add safety checks to your proportionate size methods
To avoid confusing errors if someone forgets to initialize SizeConfig, add clear checks and error messages:
// Get the proportionate height as per screen size double getProportionateScreenHeight(double inputHeight) { if (SizeConfig.screenHeight == null) { throw Exception('Oops! SizeConfig hasn\'t been initialized yet. Call SizeConfig.init(context) in your app\'s root widget first.'); } double screenHeight = SizeConfig.screenHeight!; // 812 is the layout height your designer used return (inputHeight / 812.0) * screenHeight; } // Get the proportionate width as per screen size double getProportionateScreenWidth(double inputWidth) { if (SizeConfig.screenWidth == null) { throw Exception('Oops! SizeConfig hasn\'t been initialized yet. Call SizeConfig.init(context) in your app\'s root widget first.'); } double screenWidth = SizeConfig.screenWidth!; // 375 is the layout width your designer used return (inputWidth / 375.0) * screenWidth; }
3. Optional: Add debug-mode assertions
For early detection during development, add an assertion to your helper methods. This will throw a clear warning in debug mode (but won't affect release builds):
double getProportionateScreenHeight(double inputHeight) { assert(SizeConfig.screenHeight != null, 'SizeConfig must be initialized first! Call SizeConfig.init(context) in your root widget.'); return (inputHeight / 812.0) * SizeConfig.screenHeight!; }
Once you implement these steps (especially step 1), your SizeConfig class should work smoothly without those annoying errors.
备注:内容来源于stack exchange,提问作者Jiddo

