使用 getX 注销时对空值使用的空检查运算符

时间:2021-05-25 14:52:03

标签: flutter dart flutter-test getx

因此,登录时一切正常,但注销时抛出 from google.cloud import storage import os import gcsfs import pandas as pd import pickle #read in the file print("Mod script starts") ds = pd.read_csv("gs://shottypeids/ShotTypeModel_alldata.csv") 即使注销正常,但我担心此错误会在生产模式下造成问题。

这是来自我的auth_model

的代码
_CastError

这是来自我的controller_view

的代码
Rxn<User> _user = Rxn<User>() ;


 String? get user => _user.value!.email;

 @override
 void onInit() {
  // TODO: implement onInit
   super.onInit();
   _user.bindStream(_auth.authStateChanges());
  }

这个来自我的homeScreen

 return Obx((){
  return(Get.find<AuthViewModel>().user != null)
      ? HomeScreen()
      : Home();
});

我将不胜感激。

1 个答案:

答案 0 :(得分:1)

这就是问题所在。

/// You tried to declare a private variable that might be `null`.
/// All `Rxn` will be null by default.
Rxn<User> _user = Rxn<User>();

/// You wanted to get a String from `email` property... from that variable.
/// But you also want to return `null` if it doesn't exist. see: `String?` at the beginning.
/// But you also tell dart that it never be null. see: `_user.value!`.
String? get user => _user.value!.email;

/// That line above will convert to this.
String? get user => null!.email;

您通过在下一个操作数前添加 nullnot-null 标记为 !。这就是您收到错误的原因。要解决此问题,请使用 ? 而不是 !

/// This will return `null` and ignore the next `.email` operand
/// if `_user.value` is `null`.
String? get user => _user.value?.email;