【问题标题】:getX null initial value and use '??' but getting The function can't be unconditionally invoked because it can be 'null'getX null 初始值并使用 '??'但是获取函数不能无条件调用,因为它可以是'null'
【发布时间】:2022-01-08 19:41:07
【问题描述】:

我获得了RxString? name; 的值,并想在Text() 小部件中使用,例如

Text(uController.name() ?? 'noooo nameeee'),

但是,它会导致错误

函数不能被无条件调用,因为它可以 '空值'。尝试添加一个空检查 ('!')。

即使我将? 添加到 RxString,我也不能将其用作可分配的空值。有什么方法可以将 GetX 与 ?? 命令一起使用?

// my GetxController file
class uController extends GetxController {
  RxString? name;
}

uController uController = Get.find();

我刚刚尝试了(_uController?.name() ?? 'noooo nameeee'),我仍然得到

函数不能被无条件调用,因为它可以 '空值'。尝试添加一个空检查 ('!')。

uController _uController = Get.put(uController());

class DashboardView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Obx(() => Column(
                children: [
                  Text(_uController.name() ?? 'hmm'),
                  Text(_uController?.name() ?? 'hmm'),
                ],
              )),
        ],
      ),
    )); .....

【问题讨论】:

    标签: flutter flutter-getx


    【解决方案1】:

    同时使用?.??

    Text(uController?.name() ?? 'noooo nameeee'),
    

    更多信息请阅读https://dart.dev/null-safety/understanding-null-safety

    【讨论】:

    • 我得到同样的错误 + The receiver can't be null, so the null-aware operator '?.' is unnecessary. ?. :/
    • 请分享 uController.name() 部分代码,对调试有帮助
    • 对不起,我刚刚添加了代码和截图
    • _uController.name.value 请尝试
    • 先生,您救了我 3 天 + 10 RedBull + 100 Google 搜索。非常感谢!
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2022-08-15
    • 2022-11-16
    • 1970-01-01
    • 2022-08-24
    • 2022-08-15
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多