错误“在null上调用getter'userEmail'

时间:2019-09-04 17:14:13

标签: firebase flutter firebase-authentication google-signin google-authentication

尝试使用带有Firebase的Google实现登录。有很多教程,但这是我发现最容易理解的教程。尽管我在阅读所传递的价值时遇到了一些问题。

我尝试从“ googleSignIn.dart ”调试电子邮件的值,它显示了预期的结果。

但是,如果我尝试在主屏幕中访问它,则会显示错误“ getter'userEmail'在null上被调用 ”。

这是我的 googleSignIn.dart 文件

def start():
    try:
        with open("hi.txt") as f:
            s = f.read()
            return int(s) > 0
    except Exception:
        return False

这就是我从 homeScreen.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");
}

2 个答案:

答案 0 :(得分: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),
      ),
    );
  }
}

答案 1 :(得分:0)

您的对象为空,因为您没有填充它。您必须将参数作为状态传递,并使用构造函数填充对象。

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),
      ),
    );
  }
}