未定义的类“ FirebaseUser” [英] Undefined class 'FirebaseUser'

查看:120
本文介绍了未定义的类“ FirebaseUser”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter的新手。我遇到 Firebase身份验证/ Google身份验证问题
未定义 FirebaseUser
代码:

I'm new to Flutter. I have an Issue with Firebase Auth/ Google Auth The FirebaseUser is not defined Code:

FirebaseAuth _auth = FirebaseAuth.instance;
GoogleSignIn googleSignIn = GoogleSignIn();

Future<FirebaseUser> currentUser() async { // The Issue is here in the Future<>
  final GoogleSignInAccount account = await googleSignIn.signIn();
  final GoogleSignInAuthentication authentication =
      await account.authentication;

  final GoogleAuthCredential credential = GoogleAuthProvider.getCredential(
      idToken: authentication.idToken, accessToken: authentication.accessToken);

  final AuthResult authResult = await _auth.signInWithCredential(credential);
  final FirebaseUser user = authResult.user; // and here as I can't define this FirebaseUser object to return

  return user;
}

Pubspec.yml

Pubspec.yml

dependencies:
  flutter:
    sdk: flutter


  cupertino_icons: ^0.1.3
  firebase_auth: ^0.18.0
  location: ^3.0.2
  page_transition: ^1.1.6
  google_sign_in: ^4.5.1
  flutter_facebook_login: ^3.0.0
  firebase_database: ^4.0.0

我也遇到了与AuthResult相同的问题

I also face the same issue with AuthResult

final AuthResult authResult = await _auth.signInWithCredential(credential);


推荐答案

从版本 firebase_auth 0.18.0:


在最新版本的 firebase_auth 中,类 FirebaseUser 更改为 User ,而类 AuthResult 更改为 UserCredentail 。因此,将您的代码更改为以下内容:

Starting from Version firebase_auth 0.18.0:

In the newest version of firebase_auth, the class FirebaseUser was changed to User, and the class AuthResult was changed to UserCredentail. Therefore change your code to the following:

    Future<User> currentUser() async {
      final GoogleSignInAccount account = await googleSignIn.signIn();
      final GoogleSignInAuthentication authentication =
          await account.authentication;

      final GoogleAuthCredential credential = GoogleAuthProvider.credential(
          idToken: authentication.idToken,
          accessToken: authentication.accessToken);

      final UserCredential authResult =
          await _auth.signInWithCredential(credential);
      final User user = authResult.user;

      return user;
    }




FirebaseUser 更改为 User

AuthResult 更改为 UserCredential

GoogleAuthProvider.getCredential()更改为 GoogleAuthProvider.credential( )

onAuthStateChanged 会通知用户签署的更改-状态被替换为 authStateChanges()

onAuthStateChanged which notifies about changes to the user's sign-in state was replaced with authStateChanges()

currentUser()一种检索当前登录用户的方法,已替换为属性 currentUser ,并且不再返回 Future< FirebaseUser>

currentUser() which is a method to retrieve the currently logged in user, was replaced with the property currentUser and it no longer returns a Future<FirebaseUser>.

以上两种方法的示例:

FirebaseAuth.instance.authStateChanges().listen((event) {
   print(event.email);
});

并且:

var user = FirebaseAuth.instance.currentUser;
print(user.uid);




弃用 UserUpdateInfo 类用于 firebaseUser.updateProfile 方法。
示例:


Deprecation of UserUpdateInfo class for firebaseUser.updateProfile method. Example:

Future updateName(String name, FirebaseUser user) async {
  var userUpdateInfo = new UserUpdateInfo();
  userUpdateInfo.displayName = name;
  await user.updateProfile(userUpdateInfo);
  await user.reload(); 
}

现在

import 'package:firebase_auth/firebase_auth.dart' as firebaseAuth;
Future updateName(String name, auth.User firebaseUser) async {
  firebaseUser.updateProfile(displayName: name);
  await firebaseUser.reload();
}

这篇关于未定义的类“ FirebaseUser”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆