Flutter身份验证插件shuftipro_onsite_sdk_beta的使用

Flutter身份验证插件shuftipro_onsite_sdk_beta的使用

在本示例中,我们将展示如何在Flutter应用中使用shuftipro_onsite_sdk_beta插件进行身份验证。以下是详细的步骤和代码示例。

初始化插件

首先,确保你已经在pubspec.yaml文件中添加了shuftipro_onsite_sdk_beta依赖项:

dependencies:
  shuftipro_onsite_sdk_beta: ^版本号

然后运行flutter pub get来安装该插件。

示例代码

以下是一个完整的示例,展示了如何初始化并使用shuftipro_onsite_sdk_beta插件。

main.dart

import 'package:flutter/material.dart';
import 'package:shuftipro_onsite_sdk_beta/shuftipro_onsite_sdk_beta.dart';
import 'dart:convert';

// 替换为你的客户端ID和密钥
String clientId = ""; // 进入客户端ID
String secretKey = ""; // 进入密钥

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.blue,
          scaffoldBackgroundColor: Colors.white,
          fontFamily: 'OpenSans'),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // 配置认证对象
  var authObject = {
    "auth_type": "basic_auth",
    "client_id": clientId,
    "secret_key": secretKey,
  };

  // 创建payload对象
  Map<String, Object> createdPayload = {
    "country": "",
    "language": "",
    "email": "",
    "callback_url": "",
    "redirect_url": "",
    "show_consent": 1,
    "show_privacy_policy": 1,
    "verification_mode": "image_only",
    "face": {
      "proof": "",
    },
    "document": {
      "supported_types": [
        "passport",
        "id_card",
        "driving_license",
        "credit_or_debit_card",
      ],
      "name": {
        "first_name": "",
        "last_name": "",
        "middle_name": "",
      },
      "dob": "",
      "document_number": "",
      "expiry_date": "",
      "issue_date": "",
      "gender": "",
      "backside_proof_required": "0",
    },
  };

  // 配置对象
  Map<String, Object> configObj = {
    "base_url": "api.shuftipro.com",
    "show_requirement_page": true,
  };

  /*
   * This function sends call to the Shuftipro's flutter plugin
   * and receives the response in a variable
   */

  Future<void> initPlatformState() async {
    String response = "";
    try {
      response = await ShuftiproSdk.sendRequest(
          authObject: authObject,
          createdPayload: createdPayload,
          configObject: configObj);
      var object = jsonDecode(response);
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text(object.toString()),
      ));

      print(object.toString());
      print(object["event"].toString());
    } catch (e) {
      print(e);
    }
    if (!mounted) return;
  }

  /*
   * Click listener to start the SDK flow
   */

  void continueFun() {
    var v = DateTime.now();
    var reference = "package_sample_Flutter_$v";
    createdPayload["reference"] = reference;
    initPlatformState();
  }

  /*
   * UI of demo application
   */

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: Container(
            margin: const EdgeInsets.all(10.0),
            child: OutlinedButton(
              onPressed: continueFun,
              style: OutlinedButton.styleFrom(backgroundColor: Colors.blueAccent),
              child: Container(
                alignment: Alignment.center,
                child: const Text(
                  "继续",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                    fontFamily: 'OpenSans',
                  ),
                ),
              ),
            ),
          ),
        ));
  }
}

更多关于Flutter身份验证插件shuftipro_onsite_sdk_beta的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter身份验证插件shuftipro_onsite_sdk_beta的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


shuftipro_onsite_sdk_beta 是一个用于身份验证的 Flutter 插件,通常用于集成 Shufti Pro 提供的身份验证服务。Shufti Pro 是一个提供身份验证、文件验证和生物识别等服务的平台。以下是如何在 Flutter 项目中使用 shuftipro_onsite_sdk_beta 插件的步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 shuftipro_onsite_sdk_beta 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  shuftipro_onsite_sdk_beta: ^1.0.0  # 请使用最新的版本

然后运行 flutter pub get 来获取依赖。

2. 初始化 SDK

在你的 Dart 代码中,首先需要初始化 Shufti Pro SDK。通常这会在 main.dart 或某个初始化文件中完成。

import 'package:shuftipro_onsite_sdk_beta/shuftipro_onsite_sdk_beta.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await ShuftiProOnSiteSDK.init(
    clientId: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
  );
  runApp(MyApp());
}

3. 配置身份验证请求

接下来,你需要配置身份验证请求。Shufti Pro SDK 提供了多种验证选项,例如文档验证、人脸识别等。

void startVerification() async {
  var verificationRequest = {
    'reference': 'unique_reference_id',
    'country': 'US',
    'language': 'EN',
    'callback_url': 'https://your-callback-url.com',
    'redirect_url': 'https://your-redirect-url.com',
    'verification_mode': 'any',
    'document': {
      'supported_types': ['id_card', 'passport', 'driving_license'],
      'name': {
        'first_name': 'John',
        'last_name': 'Doe',
      },
      'dob': '1990-01-01',
    },
    'face': {
      'proof': 'required',
    },
  };

  try {
    var response = await ShuftiProOnSiteSDK.requestVerification(verificationRequest);
    print('Verification Response: $response');
  } catch (e) {
    print('Error: $e');
  }
}

4. 处理回调

Shufti Pro 会在验证完成后通过 callback_urlredirect_url 发送验证结果。你需要在你的服务器上处理这些回调,或者在前端处理重定向。

5. 检查验证状态

你可以使用 reference 来检查验证的状态:

void checkVerificationStatus(String reference) async {
  try {
    var status = await ShuftiProOnSiteSDK.checkStatus(reference);
    print('Verification Status: $status');
  } catch (e) {
    print('Error: $e');
  }
}

6. 处理错误

在使用 SDK 的过程中,可能会遇到各种错误。确保你正确处理这些错误,并给用户提供适当的反馈。

try {
  var response = await ShuftiProOnSiteSDK.requestVerification(verificationRequest);
  print('Verification Response: $response');
} catch (e) {
  print('Error: $e');
  // 处理错误,例如显示错误消息给用户
}

7. 集成到 UI

最后,将身份验证流程集成到你的应用 UI 中。例如,你可以在用户点击某个按钮时触发身份验证流程。

ElevatedButton(
  onPressed: () {
    startVerification();
  },
  child: Text('Start Verification'),
);
回到顶部