尝试连接Google帐户时出现Flutter错误

时间:2020-07-29 11:35:33

标签: firebase flutter

当我尝试在应用中使用我的Google帐户登录时,在控制台enter image description here中出现此错误

应用程序关闭,我在firebase-google中使用了登录方法 enter image description here

这是我用于注册页面的代码


    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'auth.dart';
    import 'package:google_sign_in/google_sign_in.dart';

    class LoginScreen extends StatefulWidget {
      @override
      _LoginPageState createState() => _LoginPageState();
    }

    class _LoginPageState extends State<LoginScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Center(
                child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            LoginButton(), // <-- Built with StreamBuilder
            UserProfile()
          ],
        )));
      }
    }

    class UserProfile extends StatefulWidget {
      @override
      UserProfileState createState() => UserProfileState();
    }

    class UserProfileState extends State<UserProfile> {
      Map<String, dynamic> _profile;
      bool _loading = false;

      @override
      initState() {
        super.initState();

        // Subscriptions are created here
        authService.profile.listen((state) => setState(() => _profile = state));

        authService.loading.listen((state) => setState(() => _loading = state));
      }

      @override
      Widget build(BuildContext context) {
        return Column(children: <Widget>[
          Container(padding: EdgeInsets.all(20), child: Text(_profile.toString())),
          Text(_loading.toString())
        ]);
      }
    }

    class LoginButton extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return StreamBuilder(
            stream: authService.user,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return MaterialButton(
                  onPressed: () => authService.signOut(),
                  color: Colors.red,
                  textColor: Colors.white,
                  child: Text('Signout'),
                );
              } else {
                return MaterialButton(
                  onPressed: () => authService.testSignInWithGoogle(),
                  color: Colors.white,
                  textColor: Colors.black,
                  child: Text('Login with Google'),
                );
              }
            });
      }
    }

这是为了进行身份验证


    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:google_sign_in/google_sign_in.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:rxdart/rxdart.dart';

    class AuthService {
      // Dependencies
      final GoogleSignIn _googleSignIn = GoogleSignIn();
      final FirebaseAuth _auth = FirebaseAuth.instance;
      final Firestore _db = Firestore.instance;

      // Shared State for Widgets
      Observable<FirebaseUser> user; // firebase user
      Observable<Map<String, dynamic>> profile; // custom user data in Firestore
      PublishSubject loading = PublishSubject();

      // constructor
      AuthService() {
        user = Observable(_auth.onAuthStateChanged);

        profile = user.switchMap((FirebaseUser u) {
          if (u != null) {
            return _db
                .collection('users')
                .document(u.uid)
                .snapshots()
                .map((snap) => snap.data);
          } else {
            return Observable.just({});
          }
        });
      }

      Future<String> testSignInWithGoogle() async {
        final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
        final GoogleSignInAuthentication googleAuth =
            await googleUser.authentication;
        final AuthCredential credential = GoogleAuthProvider.getCredential(
          accessToken: googleAuth.accessToken,
          idToken: googleAuth.idToken,
        );
        final FirebaseUser user =
            (await _auth.signInWithCredential(credential)).user;
        assert(user.email != null);
        assert(user.displayName != null);
        assert(!user.isAnonymous);
        assert(await user.getIdToken() != null);

        final FirebaseUser currentUser = await _auth.currentUser();
        assert(user.uid == currentUser.uid);

        return 'signInWithGoogle succeeded: $user';
      }

      Future<String> signOut() async {
        try {
          await _auth.signOut();
          return 'SignOut';
        } catch (e) {
          return e.toString();
        }
      }
    }

    // TODO refactor global to InheritedWidget
    final AuthService authService = AuthService();

这就是我在pubspec.yml中拥有的内容:

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^0.4.5
  firebase_analytics: ^5.0.15

  cloud_firestore: ^0.13.6

  firebase_auth: ^0.16.1
  google_sign_in: ^4.5.1

  rxdart: ^0.22.6
  cupertino_icons: ^0.1.3

0 个答案:

没有答案