我正在尝试学习如何获取Flutter应用程序以登录Firebase身份验证。我使用android studio插件创建了一个新的flutter项目,并添加了firebase_auth page中的依赖项和代码。
我收到错误消息“尝试在FirebaseAuth.instance(_auth)中调用方法时,未为类'FirebaseAuth'定义方法'signInWithGoogle'。这是代码:
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@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);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<FirebaseUser> _handleSignIn() async {
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
FirebaseUser user = await _auth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
print("signed in " + user.displayName);
return user;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[ // Add 3 lines from here...
new IconButton(icon: const Icon(Icons.mic), onPressed: () {
_handleSignIn()
.then((FirebaseUser user) => print(user))
.catchError((e) => print(e));
})
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Pubspec.yaml:
name: flutter_auth
description: Trying out firebase_auth
version: 1.0.0+1
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
google_sign_in: ^4.0.0
firebase_auth: ^0.8.0+1
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
答案 0 :(得分:7)
我遇到了同样的问题,并在firebase_auth中找到了示例
https://github.com/flutter/plugins/blob/master/packages/firebase_auth/example/lib/main.dart
尝试将您的handleSignIn方法替换为
Future<FirebaseUser> _handleSignIn() async {
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
print("signed in " + user.displayName);
return user;
}
也许signInWithGoogle方法是有效的,但是我找不到任何方法,上面的代码对我有用。
答案 1 :(得分:3)
此实现已再次更改,因此_auth.signInWithCredential返回一个AuthResult实例,您可以作为该对象的属性访问用户。
Future<FirebaseUser> _handleSignIn() async {
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final AuthResult authResult = await _auth.signInWithCredential(credential);
FirebaseUser user = authResult.user;
print("signed in " + user.displayName);
return user;
}
答案 2 :(得分:3)
对于 Flutter 2.0
final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
await firebaseAuth.signInWithGoogle(idToken: googleAuth.accessToken, accessToken: googleAuth.idToken);
答案 3 :(得分:0)
这是使用google auth登录的方法
Future<FirebaseUser> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult =
await _firebaseAuth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
final FirebaseUser currentUser = await _firebaseAuth.currentUser();
if (currentUser != null) {
final QuerySnapshot result = await Firestore.instance
.collection('users')
.where("id", isEqualTo: currentUser.uid)
.getDocuments();
final List<DocumentSnapshot> document = result.documents;
if (document.length == 0) {
Firestore.instance
.collection('users')
.document(currentUser.uid)
.setData({
'id': currentUser.uid,
'username': currentUser.displayName,
'profilePicture': currentUser.photoUrl
});
} else {}
}
return user;
}
答案 4 :(得分:0)
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
Future<String> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
return 'signInWithGoogle succeeded: $user';
}
遵循这种方法,在我的观点中,这种方法最简单
答案 5 :(得分:0)
此答案从此处复制:Undefined class 'FirebaseUser'
从版本firebase_auth 0.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>
。
以上两种方法的示例:
FirebaseAuth.instance.authStateChanges().listen((event) {
print(event.email);
});
并且:
var user = FirebaseAuth.instance.currentUser;
print(user.uid);
答案 6 :(得分:0)
只需在全局范围内使用 FirebaseAuth 初始化 GoogleSignIn
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0.05494505 0.1098901 0.1648352 0.2197802 0.2747253 0.3296703 0.3846154 0.4395604 0.4945055
[2,] 0 0.08547009 0.1709402 0.2564103 0.3418803 0.4273504 0.5128205 0.5982906 0.6837607 0.7692308
[3,] 0 0.09473684 0.1894737 0.2842105 0.3789474 0.4736842 0.5684211 0.6631579 0.7578947 0.8526316
[4,] 0 0.09828010 0.1965602 0.2948403 0.3931204 0.4914005 0.5896806 0.6879607 0.7862408 0.8845209
[5,] 0 0.09992006 0.1998401 0.2997602 0.3996803 0.4996003 0.5995204 0.6994404 0.7993605 0.8992806
[6,] 0 0.10078387 0.2015677 0.3023516 0.4031355 0.5039194 0.6047032 0.7054871 0.8062710 0.9070549
[7,] 0 0.10128152 0.2025630 0.3038446 0.4051261 0.5064076 0.6076891 0.7089706 0.8102522 0.9115337
[8,] 0 0.10158730 0.2031746 0.3047619 0.4063492 0.5079365 0.6095238 0.7111111 0.8126984 0.9142857
[9,] 0 0.10178437 0.2035687 0.3053531 0.4071375 0.5089218 0.6107062 0.7124906 0.8142749 0.9160593
[10,] 0 0.10191602 0.2038320 0.3057481 0.4076641 0.5095801 0.6114961 0.7134121 0.8153282 0.9172442
在您编写 GoogleSignIn().signIn() 的任何地方使用 _googleSignIn
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;