Flutter现场模拟插件shuftipro_onsite_simulator的使用
Flutter现场模拟插件shuftipro_onsite_simulator的使用
在本教程中,我们将介绍如何在Flutter应用中使用shuftipro_onsite_simulator
插件。通过此插件,您可以集成Shuftipro的身份验证功能到您的应用中。
示例代码
以下是一个完整的示例代码,演示了如何在Flutter应用中使用shuftipro_onsite_simulator
插件。
import 'package:flutter/material.dart';
import 'package:shuftipro_onsite_simulator/shuftipro_onsite_simulator.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,
};
Map<String, Object> createdPayload = {
"country": "",
"language": "EN",
"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_simulator的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter现场模拟插件shuftipro_onsite_simulator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
shuftipro_onsite_simulator
是一个用于模拟 Shufti Pro 现场验证的 Flutter 插件。Shufti Pro 是一个身份验证和背景验证服务,主要用于验证用户的身份、地址等信息。shuftipro_onsite_simulator
插件允许开发者在开发环境中模拟 Shufti Pro 的现场验证过程,以便在不连接真实服务的情况下进行测试和调试。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 shuftipro_onsite_simulator
插件的依赖:
dependencies:
flutter:
sdk: flutter
shuftipro_onsite_simulator: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
使用插件
在 Flutter 项目中使用 shuftipro_onsite_simulator
插件的基本步骤如下:
-
导入插件:
在你的 Dart 文件中导入插件:
import 'package:shuftipro_onsite_simulator/shuftipro_onsite_simulator.dart';
-
初始化插件:
在应用的某个地方(例如
initState
方法中)初始化插件:ShuftiProOnsiteSimulator.initialize();
-
启动模拟验证:
使用
ShuftiProOnsiteSimulator
提供的startVerification
方法来启动模拟验证:void startSimulatedVerification() async { try { var result = await ShuftiProOnsiteSimulator.startVerification( clientId: 'your_client_id', clientSecret: 'your_client_secret', verificationData: { 'document': { 'name': 'John Doe', 'document_number': '123456789', 'country': 'US', 'dob': '1990-01-01', }, 'face': { 'image': 'base64_encoded_image', }, }, ); // 处理验证结果 print('Verification Result: $result'); } catch (e) { // 处理错误 print('Error: $e'); } }
-
处理验证结果:
startVerification
方法返回一个包含验证结果的Map
。你可以根据返回的结果来判断验证是否成功,并根据需要进行进一步处理。if (result['status'] == 'success') { // 验证成功 print('Verification succeeded!'); } else { // 验证失败 print('Verification failed: ${result['message']}'); }
注意事项
- 测试环境:
shuftipro_onsite_simulator
插件主要用于开发和测试环境。在生产环境中,你应该使用真实的 Shufti Pro 服务。 - 数据格式:确保传递给
startVerification
方法的数据格式正确,特别是verificationData
部分,因为它直接影响验证的结果。 - 错误处理:在实际应用中,务必正确处理可能出现的错误,以确保用户体验。
示例代码
以下是一个完整的示例代码,展示了如何使用 shuftipro_onsite_simulator
插件进行模拟验证:
import 'package:flutter/material.dart';
import 'package:shuftipro_onsite_simulator/shuftipro_onsite_simulator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
[@override](/user/override)
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
[@override](/user/override)
void initState() {
super.initState();
ShuftiProOnsiteSimulator.initialize();
}
void startSimulatedVerification() async {
try {
var result = await ShuftiProOnsiteSimulator.startVerification(
clientId: 'your_client_id',
clientSecret: 'your_client_secret',
verificationData: {
'document': {
'name': 'John Doe',
'document_number': '123456789',
'country': 'US',
'dob': '1990-01-01',
},
'face': {
'image': 'base64_encoded_image',
},
},
);
// 处理验证结果
print('Verification Result: $result');
if (result['status'] == 'success') {
// 验证成功
print('Verification succeeded!');
} else {
// 验证失败
print('Verification failed: ${result['message']}');
}
} catch (e) {
// 处理错误
print('Error: $e');
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Shufti Pro Simulator'),
),
body: Center(
child: ElevatedButton(
onPressed: startSimulatedVerification,
child: Text('Start Verification'),
),
),
);
}
}