未定義のクラス 'FirebaseUser'

Aug 19 2020

Flutterは初めてです。私は問題を持っているGoogleの認証/ Firebase認証ザ・FirebaseUserがコードに定義されていません。

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

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でも同じ問題に直面しています

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

回答

37 PeterHaddad Aug 19 2020 at 14:51

バージョンfirebase_auth0.18.0以降:

の最新バージョンでfirebase_authは、クラスFirebaseUserがに変更されUser、クラスAuthResultがに変更されましたUserCredentail。したがって、コードを次のように変更します。

    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<FirebaseUser>

上記の2つの方法の例:

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

そして:

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

メソッドのUserUpdateInfoクラスの非推奨firebaseUser.updateProfile。例:

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();
}
1 sleepingkit Aug 19 2020 at 14:50

実行

flutter pub get

次に、アプリを再構築します。

1 PrathamSarankar Sep 17 2020 at 02:14

これは、2020年9月の時点でメールとパスワードを使用したサインイン機能になります。Initialzeアプリは新しく導入されたメソッドであり、他のFirebaseメソッドを使用する前に少なくとも1回呼び出す必要があります。

 Future<void> signin() async {
    final formState = _formkey.currentState;
    await Firebase.initializeApp();
    if (formState.validate()) {
      setState(() {
        loading = true;
      });
      formState.save();
      try {
        print(email);
        final User user = (await FirebaseAuth.instance
                .signInWithEmailAndPassword(email: email, password: password))
            .user;
      } catch (e) {
        print(e.message);
        setState(() {
          loading = false;
        });
      }
    }
  }
1 Maann Oct 28 2020 at 13:34

最新バージョンのfirebase_authでは、クラスFirebaseUserUserに変更され、クラスAuthResultUserCredentailに変更されました。したがって、FirebaseUserUserに変更します

ChamodDissanayake Feb 04 2021 at 21:11

以来firebase_auth 0.18.0、クラスFirebaseUserはに変更されましたUser