why Flutter login with facebook is not working

10k Views Asked by At

I'm trying to make a login screen with Facebook but end up with this error when I run my code. here is my code:

 import 'package:facebook_login/services/auth_service.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_login_facebook/flutter_login_facebook.dart';
import 'package:auth/auth.dart';

class AuthBloc {
  final authService = AuthService();
  final fb = FacebookLogin();

  Stream<User> get currentUser => authService.currentUser;

  loginFacebook() async {
    print('Starting Facebook Login');

    final res = await fb.logIn(
      permissions: [
        FacebookPermission.publicProfile,
        FacebookPermission.email
      ]
    );

    switch(res.status){
      case FacebookLoginStatus.Success:
      print('It worked');

      //Get Token
      final FacebookAccessToken fbToken = res.accessToken;

      //Convert to Auth Credential
      final AuthCredential credential 
        = FacebookAuthProvider.getCredential(accessToken: fbToken.token);

      //User Credential to Sign in with Firebase
      final result = await authService.signInWithCredentail(credential);

      print('${result.user.displayName} is now logged in');

      break;
      case FacebookLoginStatus.Cancel:
      print('The user canceled the login');
      break;
      case FacebookLoginStatus.Error:
      print('There was an error');
      break;
    }
  }

  logout(){
    authService.logout();
  }
}

when i run this i get this error message

    Invalid depfile: C:\Users\Main\OneDrive\stuff\Documents\seltech\st\.dart_tool\flutter_build\7b28b646cfa2e7424746c78d0801893a\kernel_snapshot.d
Error: Couldn't resolve the package 'facebook_login' in 'package:facebook_login/services/auth_service.dart'.
Error: Couldn't resolve the package 'auth' in 'package:auth/auth.dart'.
lib/src/bloc/auth_bloc:1:8: Error: Not found: 'package:facebook_login/services/auth_service.dart'
import 'package:facebook_login/services/auth_service.dart';

there are more errors that pop up.

if you need more of the code I can send some more of it it's so much it won't fit in here

all of the error I get is from the auth_block. I have another login page made on a separate screen. I also have a manual login with firebase auth there .

2

There are 2 best solutions below

2
On

With these packages on the pubspec:

flutter_facebook_auth: ^3.5.1
firebase_auth: ^3.1.0
firebase_core: ^1.6.0

And this function:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;

Future<String?> facebookSignin() async {
    try {
      final _instance = FacebookAuth.instance;
      final result = await _instance.login(permissions: ['email']);
      if (result.status == LoginStatus.success) {
        final OAuthCredential credential =
            FacebookAuthProvider.credential(result.accessToken!.token);
        final a = await _auth.signInWithCredential(credential);
        await _instance.getUserData().then((userData) async {
          await _auth.currentUser!.updateEmail(userData['email']);
        });
        return null;
      } else if (result.status == LoginStatus.cancelled) {
        return 'Login cancelled';
      } else {
        return 'Error';
      }
    } catch (e) {
      return e.toString();
    }
  }

I'm able to login with facebook on my app

don't forget to also follow the configuration steps for android/ios and facebook for developers console

2
On

enter image description here

#enable this Permissions and features then it will work