未经手的例外:null检查操作员在null值profile_view.dart上使用:938:61)

发布于 2025-01-29 01:30:01 字数 878 浏览 3 评论 0原文

未经手的例外:

null检查运算符上使用的null值 e/flutter(31702):#0 _profileviewstate.login (软件包:.../profile/profile_view.dart:938:61)

profile_view.dart:938:61

 login(PhoneAuthCredential credential, RunMutation mutationCall) async {
    final UserCredential cr =
        await FirebaseAuth.instance.signInWithCredential(credential);
    final String firebaseToken = await cr.user!.getIdToken();
    final args = LoginArguments(firebaseToken: firebaseToken).toJson();
    final netResult = await mutationCall(args).networkResult;
    ***final loginRes = Login$Mutation.fromJson(netResult!.data!);***
    final jwt = loginRes.login.jwtToken;
    Hive.box('user').put('jwt', jwt);
    setState(() {
      verifyState = StepState.complete;
      _currentStep = 2;
    });
  }

如何解决?

Unhandled Exception:

Null check operator used on a null value
E/flutter (31702): #0 _ProfileViewState.login
(package:.../profile/profile_view.dart:938:61)

profile_view.dart:938:61

 login(PhoneAuthCredential credential, RunMutation mutationCall) async {
    final UserCredential cr =
        await FirebaseAuth.instance.signInWithCredential(credential);
    final String firebaseToken = await cr.user!.getIdToken();
    final args = LoginArguments(firebaseToken: firebaseToken).toJson();
    final netResult = await mutationCall(args).networkResult;
    ***final loginRes = Login$Mutation.fromJson(netResult!.data!);***
    final jwt = loginRes.login.jwtToken;
    Hive.box('user').put('jwt', jwt);
    setState(() {
      verifyState = StepState.complete;
      _currentStep = 2;
    });
  }

how can I solve it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2025-02-05 01:30:01

我将鼓励在不检查NULL的情况下直接使用。在这个片段中,很少有我们的零用方体。

在使用之前,请检查该变量。就像

final randomValue = .....;
if (randomValue != null) { 
   usingAfterCheckingNull  = randomValue!.variable
}

您的情况一样,摘要可能

login(PhoneAuthCredential credential, RunMutation mutationCall) async {
    final UserCredential cr =
        await FirebaseAuth.instance.signInWithCredential(credential);
    if (cr.user == null) {
      debugPrint("go null user");
      return;
    }
    final String firebaseToken = await cr.user!.getIdToken();
    final args = LoginArguments(firebaseToken: firebaseToken).toJson();
    final netResult = await mutationCall(args).networkResult;

    if (netResult != null && netResult!.data != null) {
      final loginRes = Login$Mutation.fromJson(netResult!.data!);
      final jwt = loginRes.login.jwtToken;
      Hive.box('user').put('jwt', jwt);
      setState(() {
        verifyState = StepState.complete;
        _currentStep = 2;
      });
    } else {
      debugPrint("others error");
    }
  }

I will encourage not to use ! directly without checking null. In this snippet there are few cases we can get null.

Before using ! do a null check on the variable. like

final randomValue = .....;
if (randomValue != null) { 
   usingAfterCheckingNull  = randomValue!.variable
}

In your case, snippet can be like

login(PhoneAuthCredential credential, RunMutation mutationCall) async {
    final UserCredential cr =
        await FirebaseAuth.instance.signInWithCredential(credential);
    if (cr.user == null) {
      debugPrint("go null user");
      return;
    }
    final String firebaseToken = await cr.user!.getIdToken();
    final args = LoginArguments(firebaseToken: firebaseToken).toJson();
    final netResult = await mutationCall(args).networkResult;

    if (netResult != null && netResult!.data != null) {
      final loginRes = Login$Mutation.fromJson(netResult!.data!);
      final jwt = loginRes.login.jwtToken;
      Hive.box('user').put('jwt', jwt);
      setState(() {
        verifyState = StepState.complete;
        _currentStep = 2;
      });
    } else {
      debugPrint("others error");
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文