错误“在 null 上调用了 getter 'userEmail'
Posted
技术标签:
【中文标题】错误“在 null 上调用了 getter \'userEmail\'【英文标题】:Error "The getter 'userEmail' was called on null错误“在 null 上调用了 getter 'userEmail' 【发布时间】:2020-01-07 14:57:24 【问题描述】:尝试使用 google 和 firebase 实现登录。有很多教程,但这是我发现最容易理解的一个。虽然我在读取传递的值时遇到了一些问题。
我尝试从“googleSignIn.dart”调试打印email的值,它显示了预期的结果。
但如果我尝试在主屏幕中访问它,则会显示错误“getter 'userEmail' was called on null”。
这是我的 googleSignIn.dart 文件
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:proxigram/Screens/home.dart';
import 'package:proxigram/Screens/login.dart';
import '../Models/users.dart';
import '../Models/userProvider.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
Future<FirebaseUser> signInWithGoogle(BuildContext context) async
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
UserProvider userProvider = new UserProvider(user.providerId);
if (!user.isAnonymous && user.getIdToken() != null)
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
List<UserProvider> userData = new List<UserProvider>();
userData.add(userProvider);
UserDetails details = new UserDetails(
user.providerId,
user.displayName,
user.photoUrl,
user.email,
userData,
);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomeScreen(userDetails: details)),
);
return user;
void signOutGoogle() async
await googleSignIn.signOut();
print("User Sign Out");
这就是我从 homeScreen.dart
获取数据的方式import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'dart:async';
import '../main.dart';
import '../Models/users.dart';
void main() => runApp(MyApp());
class HomeScreen extends StatefulWidget
final UserDetails userDetails;
HomeScreen(Key key, @required this.userDetails) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
class _HomeScreenState extends State<HomeScreen>
UserDetails userDetails;
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: Text(userDetails.userEmail),
),
);
【问题讨论】:
改进您的问题并发布整个代码。 抱歉回复晚了。我发布了整个代码。 【参考方案1】:您可以使用widget.userDetails.userEmail
,因为它是由StatefulWidget
构建器传递的。
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: Text(widget.userDetails.userEmail),
),
);
记住你必须初始化 obj
final UserDetails userDetails;
喜欢这个
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'dart:async';
import '../main.dart';
import '../Models/users.dart';
void main() => runApp(MyApp());
class HomeScreen extends StatefulWidget
final UserDetails userDetails;
HomeScreen(Key key, @required this.userDetails) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState(this.userDetails);
class _HomeScreenState extends State<HomeScreen>
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: Text(widget.userDetails.userEmail),
),
);
【讨论】:
【参考方案2】:您的对象为空,因为您没有填充它。您必须将参数作为参数传递给您的状态并使用构造函数来填充您的对象。
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'dart:async';
import '../main.dart';
import '../Models/users.dart';
void main() => runApp(MyApp());
class HomeScreen extends StatefulWidget
final UserDetails userDetails;
HomeScreen(Key key, @required this.userDetails) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState(this.userDetails);
class _HomeScreenState extends State<HomeScreen>
UserDetails userDetails;
_HomeScreenState(UserDetais userDetails)
this.userDetails = userDetails;
@override
Widget build(BuildContext context)
return Scaffold(
body: Center(
child: Text(userDetails.userEmail),
),
);
【讨论】:
以上是关于错误“在 null 上调用了 getter 'userEmail'的主要内容,如果未能解决你的问题,请参考以下文章