【问题标题】:Getting "The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type" error using GetX使用 GetX 获取“主体可能正常完成,导致返回 'null',但返回类型可能是不可为空的类型”错误
【发布时间】:2022-01-04 05:04:13
【问题描述】:

我收到以下错误消息:

The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.

在尝试使用以下代码使用可观察变量时:

      Obx((){ElevatedButton(
        child: Text(_authMode.value == AuthMode.Login ? 'LOGIN' : 'SIGN UP'),
        onPressed: _submit,
        style: ElevatedButton.styleFrom(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(30),
          ),
          primary: Theme.of(context).primaryColor,
          padding: const EdgeInsets.symmetric(
              horizontal: 30.0, vertical: 8.0),
          onPrimary: Theme.of(context).primaryTextTheme.button!.color,
        ))},
      ),

      Obx((){ TextButton(
        child: Text(
            '${_authMode.value == AuthMode.Login ? 'SIGN UP' : 'LOGIN'} '),
        onPressed: _switchAuthMode,
        style: TextButton.styleFrom(
          padding:
              const EdgeInsets.symmetric(horizontal: 30.0, vertical: 4),
          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          textStyle: TextStyle(color: Theme.of(context).primaryColor),
        ))
      }),

不知道怎么解决?

【问题讨论】:

    标签: flutter nullable flutter-getx obs


    【解决方案1】:

    您没有从 Obx lambda 返回任何内容。但它需要返回Widget。尝试像这样改变:

    Obx((){
         return ElevatedButton(....);});
    

    还有:

     Obx((){
         return TextButton(...);});
    

    您只是缺少 return 关键字。

    或者您可能喜欢使用不带 return 关键字的粗箭头语法,例如:

     Obx(()=> ElevatedButton(...));
    

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 2021-09-29
      • 2021-06-26
      • 2022-08-06
      • 2021-11-21
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多