Flutter数据同步插件aerosync_flutter_sdk的使用
Flutter数据同步插件aerosync_flutter_sdk的使用
介绍
本Flutter SDK提供了一个接口,通过flutter_inappwebview在Flutter应用中加载Aerosync-UI。安全地通过银行网站链接您的银行账户。通过快速、安全且令牌化的连接登录。您的信息不会被共享或出售。
1. 安装
步骤1:在pubspec.yaml
文件中添加依赖
首先,在pubspec.yaml
文件中添加aerosync_flutter_sdk
作为依赖项。我们目前正在进行持续开发以进行微调和改进,当前使用版本为^1.1.0
,它包含最新的功能和修复。
dependencies:
aerosync_flutter_sdk: ^1.1.0
步骤2:导入库
在需要使用该插件的文件中导入库。
import 'package:aerosync_flutter_sdk/aerosync_flutter_sdk.dart';
2. 使用/示例
Aerosync插件作为一个单独的窗口启动,该窗口使用Flutter InAppWebView插件并加载Aerosync的起始URL。在这个示例中,当按下ElevatedButton
时,小部件将导航到指定页面。
示例代码
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:aerosync_flutter_sdk/aerosync_flutter_sdk.dart';
void main() => runApp(UrlLauncherPage());
class UrlLauncherPage extends StatefulWidget {
UrlLauncherPage({Key? key}) : super(key: key);
@override
UrlLauncherExample createState() => UrlLauncherExample();
}
class UrlLauncherExample extends State<UrlLauncherPage> {
static const String _title = 'URL Launcher Example';
late String _token = "";
late String _env = "sandbox";
late String _deeplink = "";
late String _consumerId = "";
late bool _handleMFA = false;
late String _jobId = "";
late String _userId = "";
Map _style = {};
late bool isLoading;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: isLoading
? LaunchButton(
env: _env,
token: _token,
style: _style,
deeplink: _deeplink,
consumerId: _consumerId,
handleMFA: _handleMFA,
jobId: _jobId,
userId: _userId,
)
: SizedBox(), // 这里包括所有按钮和功能
),
);
}
@override
void initState() {
super.initState();
isLoading = false;
}
}
class LaunchButton extends StatelessWidget {
var env;
var token;
var style;
var deeplink;
var consumerId;
var handleMFA;
var jobId;
var userId;
LaunchButton(
{Key? key,
required this.env,
required this.token,
required this.style,
required this.deeplink,
this.consumerId,
this.handleMFA,
this.jobId,
this.userId})
: super(key: key);
// 处理来自aerosync的OnEvent回调
handleOnEventAerosync(eventType, data) {
}
// 处理来自aerosync的OnSuccess回调
handleOnSuccessAerosync(eventType, data) {
Map<String, dynamic> successData = jsonDecode(data);
print(data);
}
// 处理来自aerosync的OnClose回调
handleOnCloseAerosync(eventType, data) {
}
// 处理来自aerosync的OnLoad回调
handleOnLoadAerosync(eventType, data) {
}
// 处理来自aerosync的OnError回调
handleOnErrorAerosync(eventType, data) {
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () => {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => AerosyncSDKPage(
env: env,
token: token,
style: style,
onEvent: handleOnEventAerosync,
onSuccess: handleOnSuccessAerosync,
onClose: handleOnCloseAerosync,
onLoad: handleOnLoadAerosync,
onError: handleOnErrorAerosync,
deeplink: deeplink,
consumerId: consumerId,
handleMFA: handleMFA,
jobId: jobId,
userId: userId,
)))
},
child: Text(
'Connect',
style: TextStyle(fontSize: 20),
),
),
],
));
}
}
参数说明
AerosyncSDKPage
小部件接受 env
参数、token
参数、style
参数、deeplink
参数以及回调事件通知,这些对于您的实现都是必需的。要生成一个token,请查看我们的集成指南这里。
每个回调返回一个 eventType
值和从 aerosync-ui 返回的数据。
参数 | 类型 | 描述 |
---|---|---|
env |
string | 必须。可用值:staging, production。 |
token |
string | 必须。从集成指南中生成的token。 |
style * |
Map | 必须。{"width": "<double>", "height": "<double>", "bgColor": "<Hex color value in format "ARGB">"} |
onEvent |
function(response) | 必须。此方法将在用户完成银行链接工作流时触发。 |
onLoad |
function(response) | 必须。网页内容加载后调用的函数。 |
onSuccess |
function(response) | 必须。此方法将在银行成功添加后触发,并且用户点击了最终AeroSync-UI页面上的“继续”按钮。 |
onClose |
function(response) | 必须。此方法将在Aerosync小部件关闭时触发。 |
onError |
function(response) | 必须。如果AeroSync-UI发出任何错误事件,则调用此方法。 |
deeplink |
string | 必须。来自您的应用的DeepLink。 |
consumerId |
string | 唯一ID,代表客户端以应用定制。请联系团队获取更多信息。 |
handleMFA |
bool | 布尔值,确定是否调用MFA小部件。请联系团队获取更多信息。 |
jobId |
string | 唯一ID,代表当前MFA jobId。请联系团队获取更多信息。 |
userId |
string | 唯一ID,代表当前MFA userId。请联系团队获取更多信息。 |
📘 DeepLink参数是一个必填字段,用于链接回您的Flutter应用,以获得最佳的oAuth认证体验。
美国最大的金融机构使用oAuth体验来安全地验证其最终用户的银行,以提供最佳的用户体验。
要使用Flutter实现DeepLink,请参阅官方Flutter DeepLink指南这里。
存储已连接的账户
存储onSuccess()
数据属性以使用Aerosync API进行身份验证并检索账户信息。成功调用返回的数据值是一个JSON编码的值,您可以将其解码为jsonDecode(data)
以将其用作Map。查看给定示例。
{
"payload": {
"clientName": "client3",
"FILoginAcctId": "{\"u_guid\":\"USR-701a457e-5b93-4598-b7a1-b968c495ee3f\", \"m_guid\": \"MBR-d699c457-90f7-4b96-96c1-c50a445eabec\", \"a_guid\": \"ACT-9f5549d6-e402-43f4-8351-cd4018de7a80\"}",
"user_id": "a2c7f64f-3df9-4090-b3bd-ad6fc3003c90",
"user_password": "735e33b9-78ec-4887-99d7-a3056997ceb9"},
"type": "pageSuccess"
}
更多关于Flutter数据同步插件aerosync_flutter_sdk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter数据同步插件aerosync_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何使用 aerosync_flutter_sdk
进行数据同步的示例代码。请注意,这个示例假设你已经有一个正在运行的 Aerospike 数据库实例,并且已经配置好了 Aerospike 的同步服务。
首先,确保你已经在 pubspec.yaml
文件中添加了 aerosync_flutter_sdk
依赖:
dependencies:
flutter:
sdk: flutter
aerosync_flutter_sdk: ^最新版本号 # 请替换为实际最新版本号
然后运行 flutter pub get
来获取依赖。
接下来,是主要的 Flutter 代码示例,展示如何使用 aerosync_flutter_sdk
进行数据同步:
import 'package:flutter/material.dart';
import 'package:aerosync_flutter_sdk/aerosync_flutter_sdk.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
AerospikeClient? _aerospikeClient;
AerospikeSyncClient? _syncClient;
String _syncStatus = "Not Synced";
@override
void initState() {
super.initState();
_initializeAerospikeClient();
}
void _initializeAerospikeClient() async {
// 配置 Aerospike 客户端
var config = AerospikeClientConfig(
hosts: ['localhost', '127.0.0.1'], // 替换为你的 Aerospike 服务器地址
port: 3000, // Aerospike 服务器端口
namespace: 'test', // Aerospike 命名空间
setName: 'demo', // Aerospike 集合名称
userName: 'your-username', // 如果有用户名,请填写
password: 'your-password', // 如果有密码,请填写
);
_aerospikeClient = await AerospikeClient.create(config);
// 配置并初始化 AerospikeSyncClient
var syncConfig = AerospikeSyncConfig(
aerospikeClient: _aerospikeClient!,
syncSetName: 'sync_demo', // 同步集合名称
syncUrl: 'ws://localhost:9000', // 替换为你的 Aerospike Sync 服务 WebSocket URL
);
_syncClient = await AerospikeSyncClient.create(syncConfig);
// 监听同步状态变化
_syncClient!.addListener(() {
setState(() {
_syncStatus = _syncClient!.isSynced ? "Synced" : "Not Synced";
});
});
// 开始同步
await _syncClient!.start();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Aerospike Sync Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Sync Status: $_syncStatus',
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
// 示例:写入数据到 Aerospike
var key = AerospikeKey(_aerospikeClient!.namespace!, _aerospikeClient!.setName!, 'example_key');
var bins = {'bin1': 'value1', 'bin2': 42};
await _aerospikeClient!.put(null, key, bins);
// 同步数据(通常自动进行,但这里作为示例手动触发)
await _syncClient!.sync();
},
child: Text('Write and Sync Data'),
),
],
),
),
),
);
}
@override
void dispose() {
_syncClient?.close();
_aerospikeClient?.close();
super.dispose();
}
}
注意事项:
- 配置信息:确保替换示例代码中的配置信息(如服务器地址、端口、命名空间、集合名称、用户名和密码)为你的实际 Aerospike 配置。
- 依赖版本:确保
aerosync_flutter_sdk
的版本是最新的,或者至少是一个兼容的版本。 - 同步服务:确保 Aerospike Sync 服务正在运行,并且 WebSocket URL 配置正确。
- 权限:如果你的 Aerospike 集群有访问控制,确保提供的用户名和密码有适当的权限。
这个示例展示了如何初始化 Aerospike 和 AerospikeSync 客户端,如何监听同步状态,以及如何进行数据写入和同步。根据实际需求,你可能需要调整代码以适应特定的业务逻辑。