Flutter 接入 Line 登录

先创建 Line 开发者账号,去到 console 里面

Console

创建 provider

创建Channel

填入相关信息

选择Mobile App

创建

创建后跳转 LINE Login 设置

复制创建好的channel id

集成到Flutter

导入依赖

flutter_line_sdk: ^2.6.1

设置 iOS

info.plist 中添加

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <!-- Specify URL scheme to use when returning from LINE to your app. -->
      <string>line3rdp.$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    </array>
  </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
  <!-- Specify URL scheme to use when launching LINE from your app. -->
  <string>lineauth2</string>
</array>

PRODUCT_BUNDLE_IDENTIFIER替换为应用的Bundle Id

设置 Cocoapods

target 'Runner' do
+ platform :ios, '13.0'

  use_frameworks!
  use_modular_headers!
  ...

设置安卓

android {
    defaultConfig {
-        minSdk flutter.minSdkVersion
+        minSdk 24
    }
}

写代码

- void main() => runApp(MyApp());
+ void main() {
+   WidgetsFlutterBinding.ensureInitialized();
+   LineSDK.instance.setup("${your_channel_id}").then((_) {
+     print("LineSDK Prepared");
+   });
+   runApp(App());
+ }

将得到的 Channel Id 替换到上述代码中的your_channel_id

登录代码

// Line 登录
  Future<UserModel?> signInWithLine() async {
    try {
      final result = await LineSDK.instance.login();

      if (result.userProfile != null) {
        final userInfo = {
          'id': result.userProfile!.userId,
          'email': '', // Line可能不提供email,设为空字符串
          'displayName': result.userProfile!.displayName,
          'photoUrl': result.userProfile!.pictureUrl,
          'provider': 'line',
        };

        final user = UserModel.fromMap(userInfo);
        await _setCurrentUser(user, AuthProvider.line);
        return user;
      }
      return null;
    } catch (error) {
      debugPrint('Line Sign-In Error: $error');
      return null;
    }
  }
posted @ 2025-08-13 16:56  ryand  阅读(26)  评论(0)    收藏  举报