我声明
static var screenHeight;
static var screenWidth;
Run Code Online (Sandbox Code Playgroud)
然而,dart 分析将其标记为“更喜欢输入未初始化的变量和字段”。(首选输入未初始化变量...
screenHeight = MediaQuery.of(context).size.height;
screenWidth = MediaQuery.of(context).size.width;
Run Code Online (Sandbox Code Playgroud)
Dee*_*shu 18
如果变量每次都接收相同类型的值(就像 OP 的情况一样),上面标记的答案就很好,但如果情况并非如此,那么我们也可以通过使用动态 instaed var 来修复警告。
dynamic variableName;
Run Code Online (Sandbox Code Playgroud)
由于在声明变量时没有实例化,因此预先声明类型通常是一个好习惯。所以只需将 var 替换为显式类型即可。在这种情况下,将 var 替换为 double。
static double screenHeight;
static double screenWidth;
Run Code Online (Sandbox Code Playgroud)
或者,你可以这样做
static double screenHeight = MediaQuery.of(context).size.height;
static double screenWidth = MediaQuery.of(context).size.width;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10514 次 |
| 最近记录: |