getX null 初始值并使用'??'但是获取函数不能无条件调用,因为可以'null'
getX null initial value and use '??' but getting The function can't be unconditionally invoked because it can be 'null'
我已经获取了 RxString? name;
的值并想在 Text()
小部件中使用
Text(uController.name() ?? 'noooo nameeee'),
但是,它会导致
的错误
The function can't be unconditionally invoked because it can be
'null'. Try adding a null check ('!').
即使我将 ?
添加到 RxString,我也不能将其用作 null 可分配对象。有什么方法可以将 GetX 与 ??
命令一起使用?
// my GetxController file
class uController extends GetxController {
RxString? name;
}
uController uController = Get.find();
我刚试过 (_uController?.name() ?? 'noooo nameeee')
,我仍然得到
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'),
],
)),
],
),
)); .....
同时使用 ?.
和 ??
Text(uController?.name() ?? 'noooo nameeee'),
要了解更多信息,请阅读此内容https://dart.dev/null-safety/understanding-null-safety
我已经获取了 RxString? name;
的值并想在 Text()
小部件中使用
Text(uController.name() ?? 'noooo nameeee'),
但是,它会导致
的错误The function can't be unconditionally invoked because it can be 'null'. Try adding a null check ('!').
即使我将 ?
添加到 RxString,我也不能将其用作 null 可分配对象。有什么方法可以将 GetX 与 ??
命令一起使用?
// my GetxController file
class uController extends GetxController {
RxString? name;
}
uController uController = Get.find();
我刚试过 (_uController?.name() ?? 'noooo nameeee')
,我仍然得到
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'),
],
)),
],
),
)); .....
同时使用 ?.
和 ??
Text(uController?.name() ?? 'noooo nameeee'),
要了解更多信息,请阅读此内容https://dart.dev/null-safety/understanding-null-safety