Flutter:Facebook和Google身份验证 [英] Flutter: Facebook and Google Authentication

查看:94
本文介绍了Flutter:Facebook和Google身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Flutter创建的应用程序中包含Facebook和Google身份验证。有没有一个我可以用来实施的教程,因为对于如何在Flutter中包含html元素和Java语言以启用这种身份验证尚不确定。还是Flutter有一种完全不同的身份验证方法?

解决方案

添加此最新答案,因为现在有一个



因为软件包实际上在Android和iOS上使用本机Facebook登录SDK,所以这是现在要走的路。因此,没有借口使用Firebase或与自己进行交互!



希望它可以帮助遇到Facebook登录问题的其他人。并感谢包创建者 roughike



google signin使用 google_sign_in ,该软件包实际上已经相当成熟并且更容易开始。


I am trying to include Facebook and Google Authentication in my app which I am creating using Flutter. Is there a tutorial where I can utilize to implement as it is bit uncertain on how to include html elements and Javascript in Flutter to enable such authentication. Or is there a complete different way of authentication for Flutter?

解决方案

Adding this late answer since now there is a package, flutter_facebook_login that replaces flutter_facebook_connect. Here is a functioning main.dart example that should work. Just keep in mind you must have followed all configuration as described in the repository and must have a facebook app configured:

import 'package:flutter/material.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart';
import 'dart:async';

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

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

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {    
  login() async {
    final facebookLogin = new FacebookLogin();
    final result = await facebookLogin.logInWithReadPermissions(['email']);
    switch (result.status) {
      case FacebookLoginStatus.loggedIn:
        print(result.accessToken.token);
        break;
      case FacebookLoginStatus.cancelledByUser:
        print('CANCELED BY USER');
        break;
      case FacebookLoginStatus.error:
        print(result.errorMessage);
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: login,
        tooltip: 'Login With Facebook',
        child: new Icon(Icons.add),
      ),
    );
  }
}

You should see the login screen when clicking bottom right button, and check the printed response on your debug console:

This is the way to go right now since the package actually uses native Facebook Login SDKs on Android and iOS. So no excuse to use Firebase or having to interface yourself!

Hope it helps others who are having troubles with facebook login. And credits go to the package creator roughike

For google signin use google_sign_in, this package is actually quite mature and easier to get going.

这篇关于Flutter:Facebook和Google身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆