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

问题描述

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

I'm new to Flutter. I have an Issue with Firebase Auth/ Google AuthThe FirebaseUser is not definedCode:

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 类更改为 UserAuthResult 类已更改为 UserCredential.因此,将您的代码更改为以下内容:

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 UserCredential. 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()

currentUser() 是一种检索当前登录用户的方法,被替换为属性 currentUser 并且不再返回 Future;.

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


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


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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:15