我正在使用Flutter制作我的应用程序,我希望使用Facebook进行功能登录,这就是我所做的:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_login_facebook/flutter_login_facebook.dart';
abstract class AuthBase {
User get currentUser;
Stream<User> authStateChanges();
Future<User> signInWithFacebook();
Future<void> signOut();
}
class Auth implements AuthBase {
final _firebaseAuth = FirebaseAuth.instance;
@override
Stream<User> authStateChanges() => _firebaseAuth.authStateChanges();
@override
User get currentUser => _firebaseAuth.currentUser;
@override
Future<User> signInWithFacebook() async {
final fb = FacebookLogin();
final response = await fb.logIn(permissions: [
FacebookPermission.publicProfile,
FacebookPermission.email,
]);
switch (response.status) {
case FacebookLoginStatus.Success:
final accessToken = response.accessToken;
final userCredential = await _firebaseAuth.signInWithCredential(
FacebookAuthProvider.credential(accessToken.token),
);
return userCredential.user;
case FacebookLoginStatus.Cancel:
throw FirebaseAuthException(
code: 'ERROR_ABORTED_BY_USER',
message: 'Sign in aborted by user',
);
case FacebookLoginStatus.Error:
throw FirebaseAuthException(
code: 'ERROR_FACEBOOK_LOGIN_FAILED',
message: response.error.developerMessage,
);
default:
throw UnimplementedError();
}
}
@override
Future<void> signOut() async {
final facebookLogin = FacebookLogin();
await facebookLogin.logOut();
await _firebaseAuth.signOut();
}
}
这是我得到的错误:
Performing hot restart...
Syncing files to device AOSP on IA Emulator...
lib/services/auth.dart:68:32: Error: Getter not found: 'Success'.
case FacebookLoginStatus.Success:
^^^^^^^
lib/services/auth.dart:74:32: Error: Getter not found: 'Cancel'.
case FacebookLoginStatus.Cancel:
^^^^^^
lib/services/auth.dart:79:32: Error: Getter not found: 'Error'.
case FacebookLoginStatus.Error:
^^^^^
Restarted application in 278ms.
我试着看了一下教程,我发现我写的代码没有什么不同,它在Udemy上,所以我不能把这里的链接复制给你。请告诉我如何处理,非常感谢,祝度过美好的一天
将代码更改为
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_login_facebook/flutter_login_facebook.dart';
abstract class AuthBase {
User get currentUser;
Stream<User> authStateChanges();
Future<User> signInWithFacebook();
Future<void> signOut();
}
class Auth implements AuthBase {
final _firebaseAuth = FirebaseAuth.instance;
@override
Stream<User> authStateChanges() => _firebaseAuth.authStateChanges();
@override
User get currentUser => _firebaseAuth.currentUser;
@override
Future<User> signInWithFacebook() async {
final fb = FacebookLogin();
final response = await fb.logIn(permissions: [
FacebookPermission.publicProfile,
FacebookPermission.email,
]);
switch (response.status) {
case FacebookLoginStatus.success:
final accessToken = response.accessToken;
final userCredential = await _firebaseAuth.signInWithCredential(
FacebookAuthProvider.credential(accessToken.token),
);
return userCredential.user;
case FacebookLoginStatus.cancel:
throw FirebaseAuthException(
code: 'ERROR_ABORTED_BY_USER',
message: 'Sign in aborted by user',
);
case FacebookLoginStatus.error:
throw FirebaseAuthException(
code: 'ERROR_FACEBOOK_LOGIN_FAILED',
message: response.error.developerMessage,
);
default:
throw UnimplementedError();
}
}
@override
Future<void> signOut() async {
final facebookLogin = FacebookLogin();
await facebookLogin.logOut();
await _firebaseAuth.signOut();
}
}
小写此代码:
case FacebookLoginStatus.Success: to case FacebookLoginStatus.success:
case FacebookLoginStatus.Cancel: to case FacebookLoginStatus.cancel:
case FacebookLoginStatus.Error: to case FacebookLoginStatus.error:
请尝试这段code
,它对我来说很好。
fbSignIn() async {
var facebookLogin = FacebookLogin();
var facebookLoginResult = await facebookLogin.logIn(['email']);
switch (facebookLoginResult.status) {
case FacebookLoginStatus.error:
print("Error");
//showToast(msg: "A Error Occured");
break;
case FacebookLoginStatus.cancelledByUser:
print("CancelledByUser");
//showToast(msg: "Login Cancelled");
break;
case FacebookLoginStatus.loggedIn:
print(facebookLoginResult.accessToken.token);
print("LoggedIn");
break;
}
}