Flutter未知功能探索插件motor_flutter的使用
Flutter未知功能探索插件motor_flutter的使用
安装说明
要安装 motor_flutter
插件,请按照以下步骤操作:
-
使用 Flutter CLI:
flutter pub add motor_flutter
-
或者在
pubspec.yaml
文件中添加依赖项:dependencies: motor_flutter: ^0.9.2
-
更新 iOS Podfile(如果需要):
platform :ios, '13.0' # 在文件顶部 . . use_frameworks! # 然后删除或注释掉
使用说明
motor_flutter
提供了完整的使用示例,可以在其文档网站上找到。每个 MotorFlutter
类的方法都有详细的示例和使用指南。
示例代码
// ignore_for_file: use_build_context_synchronously
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:motor_flutter/motor_flutter.dart';
Future<void> main() async {
// 初始化服务
WidgetsFlutterBinding.ensureInitialized();
await MotorFlutter.init();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late final WhoIs? whoIs;
WhatIs? testWhatIs;
Schema? testSchema;
String? testSchemaDid;
SchemaDocument? testDocument;
Bucket? testBucket;
late final List<int>? dscKey;
late final List<int>? pskKey;
String titleMsg = "Unauthorized";
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Motor Flutter Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text(titleMsg),
// actions: [
// IconButton(
// //
// // 2. 登录到新账户
// //
// onPressed: () async {
// // 这行代码是不必要的,开发者需要提供一个UI来输入密码和账户地址。在生产模式下,dscKey 和 pskKey 会存储在设备密钥链中。
// if (whoIs == null) {
// Get.snackbar("Error", "WhoIs 字段尚未设置");
// return;
// }
// final res = await MotorFlutter.to.login(
// password: "hard-to-hack-password",
// address: whoIs!.owner,
// dscKey: dscKey,
// pskKey: pskKey,
// );
// whoIs = res;
// Get.snackbar("Error", "Failed to login user");
// return;
// },
// icon: const Icon(Icons.login))
// ],
),
body: Center(
child: Column(
children: [
//
// 1. 注册新账户
//
// 当在调试模式下运行应用程序时,模拟器中的设备密钥链不可用。我们提供了一个回调函数,用于安全地存储您的密钥。
// Sonr 团队推荐使用以下两个包之一来安全地存储您的密钥:
// - [biometric_storage] https://pub.dev/packages/biometric_storage
// - [flutter_keychain] https://pub.dev/packages/flutter_keychain
ContinueOnSonrButton(
variant: ButtonVariant.black,
onSuccess: (authInfo) {
if (kDebugMode) {
print(authInfo.toString());
}
setState(() {
if (authInfo != null) {
titleMsg = authInfo.address;
}
});
},
onError: (err) {
if (kDebugMode) {
print(err);
}
},
),
//
// 3. 尝试创建一个 Schema
//
MaterialButton(
child: const Text("New Example Schema"),
onPressed: () async {
// 设置标签,然后是一个映射,其中包含属性名称及其关联的基本类型。
final res = await MotorFlutter.to.publishSchema(
"Profile",
Map<String, SchemaFieldKind>.from({
"name": SchemaFieldKind(kind: Kind.STRING),
"age": SchemaFieldKind(kind: Kind.INT),
"fav_color": SchemaFieldKind(kind: Kind.STRING),
}));
if (kDebugMode) {
print(res.toString());
}
setState(() {
testWhatIs = res.whatIs;
test Schema = res.whatIs.schema;
test SchemaDid = res.whatIs.did;
});
},
),
//
// 4. 让我们根据该 schema 创建一个文档
//
MaterialButton(
child: const Text("Build Document from Schema"),
onPressed: () async {
if (test Schema == null) {
Get.snackbar(
"Error",
"Failed to create schema",
backgroundColor: Colors.red,
colorText: Colors.white,
);
return;
}
test Schema!.printToConsole();
final newDoc = test Schema!.newDocument("Todds Profile");
newDoc.set<String>("name", "Todd");
newDoc.set<int>("age", 24);
if (kDebugMode) {
print(newDoc.toString());
}
setState(() {
test Document = newDoc;
});
},
),
//
// 5. 创建一个桶并将其添加到最近创建的文档中
//
MaterialButton(
child: const Text("Create Bucket & Add Document"),
onPressed: () async {
test Bucket =
await MotorFlutter.to.createBucket("Test Bucket");
final item = await test Bucket!.add(test Document!);
if (item == null) {
Get.snackbar(
"Error",
"Failed to Upload testDocument to bucket",
backgroundColor: Colors.red,
colorText: Colors.white,
);
return;
}
Get.snackbar("Success",
"Uploaded document to user encrypted IPFS Store. CID: ${item.uri}");
},
),
const LoginButton()
],
),
),
),
);
}
}
更多关于Flutter未知功能探索插件motor_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复