Home > Net >  getX null initial value and use '??' but getting The function can't be unconditionall
getX null initial value and use '??' but getting The function can't be unconditionall

Time:12-03

I have get value of RxString? name; and want to use in the Text() widget like

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

However, it causes an error of

The function can't be unconditionally invoked because it can be 'null'. Try adding a null check ('!').

Even if I add ? to RxString, I can't use it as null assignable. Is there any way that I can use GetX with ?? command?

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

uController uController = Get.find();

I'd just tried with (_uController?.name() ?? 'noooo nameeee'), I still get

The function can't be unconditionally invoked because it can be 'null'. Try adding a null check ('!').

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'),
                ],
              )),
        ],
      ),
    )); .....

enter image description here

CodePudding user response:

Use both ?. and ??

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

For more read this https://dart.dev/null-safety/understanding-null-safety

  • Related