由于提供程序类而对空值使用空检查运算符

发布于 2025-01-17 19:31:11 字数 619 浏览 0 评论 0原文

当我尝试访问特定的屏幕时,我将获得“零检查操作员”。

我阻止了我的“ USER_PROVIDER”自定义类别以及使用的代码部分,并且它正常显示(无红屏幕),但没有这些屏幕。

这是定制类

import 'package:flutter/widgets.dart';
import 'package:purple/models/user.dart';
import 'package:purple/resources/auth_methods.dart';

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  User get getUser => _user!;

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

“ Undertand”,这可能是第9行中的爆炸操作员的结果,但是我是Fluttr的新手,不知道该如何处理。

I'm getting the 'null check operator used on a null value' when I try to access specific screens.

I blocked out my 'user_provider' custom class as well as parts of the code where it's used and it displays normally (no red screen) but without those screens.

This is the custom class

import 'package:flutter/widgets.dart';
import 'package:purple/models/user.dart';
import 'package:purple/resources/auth_methods.dart';

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  User get getUser => _user!;

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

I underdertand this could be as a result of the bang operator in line 9 but I'm new to fluttr and have no idea how to go about it.

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

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

发布评论

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

评论(2

情域 2025-01-24 19:31:11

如果返回null,则不应调用getuser。也许您没有从_authmethods.getuserdetails()中加载用户。

因此,这是一个想法,请检查用户是否先登录,然后调用getuser

import 'package:flutter/widgets.dart';
import 'package:purple/models/user.dart';
import 'package:purple/resources/auth_methods.dart';

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  User get getUser => _user!;

  bool get isLoggedIn => _user != null; // <---------- added

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

然后在屏幕或小部件中:

if (userProvider.isLoggedIn) {
   // TODO: do what you want such as returning user's name
   return userProvider.getUser.name;
}

You should not call getUser if it returns null. Maybe you did not load the user from _authMethods.getUserDetails().

So here is an idea, check if user is logged in first then call getUser

import 'package:flutter/widgets.dart';
import 'package:purple/models/user.dart';
import 'package:purple/resources/auth_methods.dart';

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  User get getUser => _user!;

  bool get isLoggedIn => _user != null; // <---------- added

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

then in a screen or a widget:

if (userProvider.isLoggedIn) {
   // TODO: do what you want such as returning user's name
   return userProvider.getUser.name;
}
暖心男生 2025-01-24 19:31:11

尝试以下操作:

import 'package:flutter/widgets.dart';
import 'package:purple/models/user.dart';
import 'package:purple/resources/auth_methods.dart';

class UserProvider with ChangeNotifier {
  User _user;
  final AuthMethods _authMethods = AuthMethods();

  User get getUser => _user;

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

Try this:

import 'package:flutter/widgets.dart';
import 'package:purple/models/user.dart';
import 'package:purple/resources/auth_methods.dart';

class UserProvider with ChangeNotifier {
  User _user;
  final AuthMethods _authMethods = AuthMethods();

  User get getUser => _user;

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文