尝试使用firebase_auth

时间:2019-05-24 12:31:20

标签: flutter firebase-authentication google-signin

我试图使用firebase_auth在手机上使用我的Google帐户登录,而在选择我的Google帐户后,就会发生此错误。 我正在使用以下插件:

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  provider: ^2.0.1
  firebase_auth: ^0.11.1
  google_sign_in: ^4.0.1+3

我创建了一个Firebase项目。 注册了我的Android应用。 新增SHA-1 对两个build.gradle文件进行了所有更改。 google-services.json文件也就位。

代码

import 'package:flutter/material.dart';

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String name = '';

  Future signIn() async {
    final GoogleSignIn _googleSignIn = GoogleSignIn();
    final FirebaseAuth _auth = FirebaseAuth.instance;

    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);
    setState(() {
      name = user.displayName;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '$name',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: signIn,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

错误:

[+3146 ms] E/flutter (13503): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception:
PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 12500: , null)

1 个答案:

答案 0 :(得分:1)

答案实际上很简单。我所要做的就是在Firebase项目设置中添加支持电子邮件。感谢this comment。此外,现在官方文档https://pub.dev/packages/firebase_auth

中对此进行了更新。