由于提供程序类而对空值使用空检查运算符
当我尝试访问特定的屏幕时,我将获得“零检查操作员”。
我阻止了我的“ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果返回null,则不应调用
getuser
。也许您没有从_authmethods.getuserdetails()
中加载用户。因此,这是一个想法,请检查用户是否先登录,然后调用
getuser
,然后在屏幕或小部件中:
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
then in a screen or a widget:
尝试以下操作:
Try this: