Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter flutter logo facebook login

a flutter plugin for allowing users to authenticate with native android & ios facebook login sdks.

a flutter plugin for using the native facebook login sdks on android and ios.

this plugin uses the new gradle 4.1 and android studio 3.0 project setup.

i created this library out of necessity, as there was nothing that fit my needs at the time. i needed something that was well tested, offered as much control as the native facebook login sdks, and also had good code quality.

flutter facebook login sdk (2os)
flutter facebook login sdks

dart support

  • dart 1: 1.0.x.
  • dart 2: 1.1.0 and up.

how do i use it?

the library tries to closely match the native android & ios login sdk apis where possible. for complete api documentation, just see the . everything is documented there.

since sample code is worth more than one page of documentation, here are the usual cases covered:

import 'package:flutter_facebook_login/flutter_facebook_login.dart';

var facebooklogin = new facebooklogin();
var result = await facebooklogin.loginwithreadpermissions(['email']);

switch (result.status) {
  case facebookloginstatus.loggedin:
    _sendtokentoserver(result.accesstoken.token);
    _showloggedinui();
    break;
  case facebookloginstatus.cancelledbyuser:
    _showcancelledmessage();
    break;
  case facebookloginstatus.error:
    _showerroronui(result.errormessage);
    break;
}

you can also change the visual appearance of the login dialog. for example:

// let's force the users to login using the login dialog based on webviews. yay!
facebooklogin.loginbehavior = facebookloginbehavior.webviewonly;

the complete api documentation lives with the source code, which can be found here.

getting the facebook profile of a signed in user

for now, this feature isn’t going to be integrated into this plugin. see the discussion here.

however, you can get do this in four lines of dart code:

var result = await facebooksignin.loginwithreadpermissions(['email']);
var accesstoken = result.accesstoken;
var graphresponse = await http.get(
            'https://graph.facebook.com/v2.12/me?fields=name,first_name,last_name,email&access_token=${accesstoken.token}');
var profile = json.decode(graphresponse.body);

the profile variable will now contain the following information:

{
   "name": "iiro krankka",
   "first_name": "iiro",
   "last_name": "krankka",
   "email": "iiro.krankkau0040gmail.com",
   "id": "<user id here>"
}

installation

to get things up and running, you’ll have to declare a pubspec dependency in your flutter project.
also some minimal android & ios specific configuration must be done, otherise your app will crash.

on your flutter project

see the installation instructions on pub.

android

this assumes that you’ve done the “associate your package name and default class with your app” and
“provide the development and release key hashes for your app” in the the facebook login documentation for android site.

after you’ve done that, find out what your facebook app id is. you can find your facebook app id in your facebook app’s dashboard in the facebook developer console.

once you have the facebook app id figured out, youll have to do two things.

first, copy-paste the following to your strings resource file. if you don’t have one, just create it.

<your project root>/android/app/src/main/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">your app name here.</string>

    <!-- replace "000000000000" with your facebook app id here. -->
    <string name="facebook_app_id">000000000000</string>
    
    <!-- replace "000000000000" with your facebook app id here. -->
    <string name="fb_login_protocol_scheme">fb000000000000</string>
</resources>

then you’ll just have to copy-paste the following to your android manifest:

<your project root>/android/app/src/main/androidmanifest.xml

<meta-data android:name="com.facebook.sdk.applicationid" 
    android:value="@string/facebook_app_id"/>

<activity android:name="com.facebook.facebookactivity"
    android:configchanges=
            "keyboard|keyboardhidden|screenlayout|screensize|orientation"
    android:label="@string/app_name" />

<activity
    android:name="com.facebook.customtabactivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.view" />
        <category android:name="android.intent.category.default" />
        <category android:name="android.intent.category.browsable" />
        <data android:scheme="@string/fb_login_protocol_scheme" />
    </intent-filter>
</activity>

a sample of a complete androidmanifest file can be found here.

done!

ios

this assumes that you’ve done the “register and configure your app with facebook” step in the
the facebook login documentation for ios site.

after you’ve done that, find out what your facebook app id is. you can find your facebook app id in your facebook app’s dashboard in the facebook developer console.

once you have the facebook app id figured out, then you’ll just have to copy-paste the following to your info.plist file, before the ending </dict></plist> tags.

<your project root>/ios/runner/info.plist

<key>cfbundleurltypes</key>
<array>
    <dict>
        <key>cfbundleurlschemes</key>
        <array>
            <!-- replace "000000000000" with your facebook app id here. -->
            <string>fb000000000000</string>
        </array>
    </dict>
</array>

<key>facebookappid</key>

<!-- replace "000000000000" with your facebook app id here. -->
<string>000000000000</string>
<key>facebookdisplayname</key>

<!-- replace "your_app_name" with your app name. -->
<string>your_app_name</string>

<key>lsapplicationqueriesschemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-share-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

a sample of a complete info.plist file can be found here.

download the full project for this post from the following button

this source is fully free for all time

download as zip


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.