不能无条件调用运算符“*”,因为接收者可以为“null”。尝试向目标添加空检查('!')

Posted

技术标签:

【中文标题】不能无条件调用运算符“*”,因为接收者可以为“null”。尝试向目标添加空检查(\'!\')【英文标题】:The operator '*' can't be unconditionally invoked because the receiver can be 'null'. Try adding a null check to the target ('!')不能无条件调用运算符“*”,因为接收者可以为“null”。尝试向目标添加空检查('!') 【发布时间】:2021-07-03 06:18:50 【问题描述】:

我将此代码用于我的 UI 中的响应性。所以这段代码基本上做的是计算屏幕的大小,我使用下面的函数根据在 Figma 或 Adob​​e XD 中提供给我的设计来放置确切的字体大小。使用这种方法,我能够创建像素完美的 UI。

升级到Flutter 2.0.3 后,我收到了空安全错误。我能够解决其中的大部分问题,但我无法解决此错误。 请指教。

Complete Code

import 'package:flutter/material.dart';

class SizeConfig 
  static MediaQueryData? _mediaQueryData;
  static double? screenWidth;
  static double? screenHeight;
  static double? defaultSize;
  static Orientation? orientation;

  void init(BuildContext context) 
    _mediaQueryData = MediaQuery.of(context);
    screenWidth = _mediaQueryData!.size.width;
    screenHeight = _mediaQueryData!.size.height;
    orientation = _mediaQueryData!.orientation;
    if (orientation == Orientation.landscape) 
      defaultSize = screenHeight! * 0.024;
     else 
      defaultSize = screenWidth! * 0.024;
    
  


double getSize(double size) 
  var defaultsSize = SizeConfig.defaultSize * size;
  return (defaultsSize / 10);


// Get the proportionate height as per screen size
double getProportionateScreenHeight(double inputHeight) 
  double screenHeight = SizeConfig.screenHeight!;
  // 812 is the layout height that designer use
  return (inputHeight / 812.0) * screenHeight;


// Get the proportionate width as per screen size
double getProportionateScreenWidth(double inputWidth) 
  double screenWidth = SizeConfig.screenWidth!;
  // 375 is the layout width that Figma provides
  return (inputWidth / 375.0) * screenWidth;

Error

【问题讨论】:

改成SizeConfig.defaultSize! * size 所以你成功地解决了同样的问题两次,但不知何故忘记了如何为你的第三种方法做到这一点? 【参考方案1】:

因为SizeConfig.defaultSize 可以为空,所以需要确保它的值不应该为空。

你可以添加一些断言来通知调用者应该首先初始化SizeConfig。然后,您可以将其更改为SizeConfig.defaultSize!

样品...

double getSize(double size) 
  assert(
    SizeConfig.defaultSize != null,
    "SizeConfig should be initialized (only once) before calling getSize(...). Refer to SizeConfig.init(...).",
  );
  var defaultsSize = SizeConfig.defaultSize! * size;
  return (defaultsSize / 10);

【讨论】:

【参考方案2】:

问题:

您收到此错误是因为您正在调用 * 的对象可能是 null

例子:

int? count = 1;

void main() 
  print(count * 2); // error: The operator '*' can't be unconditionally invoked ...


解决方案:

使用局部变量:

int? count = 1;

void main() 
  var i = count;
  if (i != null) 
    print(i * 2); // Prints 2
  

使用 bang 运算符 (!)

int? count = 1;

void main() 
  print(count! * 2); // Prints 2

【讨论】:

【参考方案3】:

您有三个选择:

使用 bang 运算符:
int? count = 1;

void main() 
  // will throw an error if count is null
  print(count! * 2);

使用 ??运营商:
int? count = 1;

void main() 
  // safe
  print((count ?? 1) * 2);

使用 if - else 语句:
int? count = 1;

void main() 
  if(count != null) 
    print(count! * 2);
   else 
    print('count is null');
  

【讨论】:

以上是关于不能无条件调用运算符“*”,因为接收者可以为“null”。尝试向目标添加空检查('!')的主要内容,如果未能解决你的问题,请参考以下文章

无法无条件调用方法“[]”,因为接收者可以为“null”

不能无条件访问属性“长度”,因为接收者可以为“空”。尝试使访问有条件(使用'?.')或添加

Flutter - '属性'设置'不能无条件访问,因为接收器可以是'null''

无法无条件访问属性“uid”,因为接收者可以为“null”

JAVA if判断新手求解。

Java 运算符检查任一条件是不是为假,但不能同时为假或都不为假