Android使用Apple和Firebase进行登录的Flutter
创始人
2024-10-10 13:31:10
0

要在Flutter中使用Firebase进行登录,并且支持Apple登录,你可以按照以下步骤进行操作:

  1. 添加依赖项: 在Flutter项目的pubspec.yaml文件中,添加以下依赖项:
dependencies:
  firebase_core: ^0.4.0+9
  firebase_auth: ^0.14.0+5
  google_sign_in: ^4.0.7
  apple_sign_in: ^0.1.0

运行flutter pub get以获取依赖项。

  1. 配置Firebase项目: 在Firebase控制台创建一个新的项目,并将Android和iOS应用添加到该项目中。在Android应用中,下载并添加google-services.json文件。在iOS应用中,下载并添加GoogleService-Info.plist文件。

  2. 配置Android项目: 在Android项目的android/build.gradle文件中,添加以下代码:

dependencies {
    // ...
    classpath 'com.google.gms:google-services:4.2.0'
}

在Android项目的android/app/build.gradle文件的底部添加以下代码:

apply plugin: 'com.google.gms.google-services'
  1. 配置iOS项目: 在iOS项目的ios/Podfile文件中添加以下代码:
pod 'Firebase/Auth'

然后运行以下命令来安装依赖项:

cd ios
pod install
  1. 配置Flutter代码: 在Flutter项目的lib/main.dart文件中,添加以下代码示例:
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:apple_sign_in/apple_sign_in.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

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

class LoginPage extends StatelessWidget {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final GoogleSignIn _googleSignIn = GoogleSignIn();

  Future signInWithGoogle() async {
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    final AuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    final UserCredential userCredential = await _auth.signInWithCredential(credential);
    return userCredential;
  }

  Future signInWithApple() async {
    final AuthorizationResult result = await AppleSignIn.performRequests([
      AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
    ]);

    switch (result.status) {
      case AuthorizationStatus.authorized:
        final AuthCredential credential = OAuthProvider('apple.com').credential(
          idToken: String.fromCharCodes(result.credential.identityToken),
          accessToken: String.fromCharCodes(result.credential.authorizationCode),
        );

        final UserCredential userCredential = await _auth.signInWithCredential(credential);
        return userCredential;
      case AuthorizationStatus.error:
        throw FirebaseAuthException(
          code: 'apple_sign_in_failed',
          message: result.error.toString(),
        );
      case AuthorizationStatus.cancelled:
        throw FirebaseAuthException(
          code: 'apple_sign_in_cancelled',
          message: 'Sign in with Apple has been cancelled by the user.',
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Firebase Login'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              onPressed: () async {
                try {
                  final UserCredential userCredential = await signInWithGoogle();
                  print('User signed in with Google: ${userCredential.user.displayName}');
                } catch (e) {
                  print('Failed to sign in with Google: $e');
                }
              },
              child: Text('Sign in with Google'),
            ),
            RaisedButton(
              onPressed: () async {
                try {
                  final UserCredential userCredential = await signInWithApple();
                  print('User signed in with Apple: ${userCredential.user.displayName}');
                } catch (e) {
                  print('Failed to sign in with Apple: $e');
                }
              },
              child: Text('Sign in with Apple'),

相关内容

热门资讯

程序员教你!德州局怎么透视,p... 程序员教你!德州局怎么透视,poker辅助器免费安装,总是是真的有挂(的确有挂)1)有没有挂:进一步...
热点推荐!wpk私人辅助,po... 热点推荐!wpk私人辅助,pokemmo手机版脚本免费,切实是有挂(有挂讲解)辅助器是一种具有地方特...
总算了解!wepoker辅助器... 总算了解!wepoker辅助器安装包,wepoker辅助器安装包,其实是真的有挂(揭秘有挂)1、玩家...
发现玩家!wejoker私人辅... 发现玩家!wejoker私人辅助软件,aapoker插件,果然是真的有挂(有挂秘笈)在进入软件靠谱后...
热门推荐!wepoker轻量版... 热门推荐!wepoker轻量版有透视吗,德普之星有透视辅助吗,其实是有挂(有挂方略)一、游戏安装教程...
发现玩家!wepoker怎么挂... 发现玩家!wepoker怎么挂底牌,wepoker智能辅助插件,确实是有挂(有挂助手)小薇(辅助器软...
查到实测!wepoker私人定... 查到实测!wepoker私人定制透视,wepoker插件功能辅助器,切实是真的有挂(有挂方略)是不是...
科普分享!拱趴大菠萝十三水作弊... 科普分享!拱趴大菠萝十三水作弊,wepoker是不是有人用挂,其实真的是有挂(证实有挂)1、让任何用...
分享实测!wpk刷入池率脚本,... 分享实测!wpk刷入池率脚本,wepoker破解器,本来真的有挂(有挂教学)1、操作简单,无需手机版...
我来教教大家!德州辅助工具到底... 我来教教大家!德州辅助工具到底怎么样,智星德州辅助译码插件靠谱吗,总是是真的有挂(有挂分析)1、这是...