Flutter集成Appwrite插件appwritex的使用

发布于 1周前 作者 h691938207 来自 Flutter

Flutter集成Appwrite插件appwritex的使用

[测试版] AppwriteX在不侵入的情况下为Appwrite Dart SDK提供了额外的功能。

版本 持续集成 许可证

特性

  • AppwriteX 扩展了Appwrite Dart SDK的功能。

开始使用

  1. 在你的pubspec.yaml文件中添加此包:
dependencies:
  dart_appwrite: ^<最新版本>
  appwritex: ^1.0.0
  1. 运行dart pub get安装包。
  2. 在你的Dart代码中导入包:
import 'package:dart_appwrite/dart_appwrite.dart';
import 'package:appwritex/appwritex.dart';
  1. 在你的代码中使用包:
void main() async {
  final client = Client()
      .setEndpoint('http://localhost/v1')
      .setProject('fedcba9876543210')
      .setKey('0123456789abcdef');
  final users = Users(client);
  final usersUsage = await users.getUsage();    // users.getUsage() 是由AppwriteX提供的
  print(usersUsage.toMap());
}

类和方法

// 头像
Avatars.getCardsCloud();         // 获取特定用户ID的卡片
Avatars.getCardsCloudBack();     // 获取特定用户ID的卡片背面
Avatars.getCardsCloudOG();       // 获取特定用户ID的卡片OG

// 控制台
Console.getVariables();          // 获取所有控制台变量

// 数据库
Databases.getCollectionUsage();  // 获取特定集合的使用情况
Databases.getDatabaseUsage();    // 获取特定数据库的使用情况
Databases.getUsage();            // 获取所有数据库的使用情况

// 函数
Functions.getFunctionUsage();    // 获取特定函数的使用情况
Functions.getUsage();            // 获取所有函数的使用情况

// 健康
Health.getStats();               // 获取健康统计信息

// 消息传递
Messaging.getUsage();            // 获取所有消息传递的使用情况

// 项目
Project.getVariables();          // 获取特定项目的变量
Project.getUsage();              // 获取特定项目的使用情况

// 项目列表
Projects.list();                 // 列出所有项目

// 代理
Proxy.listRules();               // 列出所有代理规则

// 公共
Public.getVersions();            // 获取所有版本信息
Public.getHealthVersion();       // 获取健康版本信息

// 存储
Storage.getBucketUsage();        // 获取特定存储桶的使用情况
Storage.getUsage();              // 获取所有存储的使用情况

// 团队
Teams.getUsage();                // 获取所有团队的使用情况
Teams.listLogs();                // 列出所有团队日志

// 用户
Users.getUsage();                // 获取所有用户的使用情况

// VCS
Vcs.listInstallations();         // 列出所有VCS安装

你也可以使用Usage类来获取所有服务的使用情况。Usage类是一个包装类,提供了一个方法来获取所有服务的使用情况。

// 使用情况
Usage.getCollectionUsage(); // 获取特定集合的使用情况
Usage.getDatabaseUsage();   // 获取特定数据库的使用情况
Usage.getDatabasesUsage();  // 获取所有数据库的使用情况

Usage.getFunctionUsage();   // 获取特定函数的使用情况
Usage.getFunctionsUsage();  // 获取所有函数的使用情况

Usage.getMessagingUsage();  // 获取所有消息传递的使用情况

Usage.getProjectUsage();    // 获取特定项目的使用情况

Usage.getBucketUsage();     // 获取特定存储桶的使用情况
Usage.getStorageUsage();    // 获取所有存储的使用情况

Usage.getTeamsUsage();      // 获取所有团队的使用情况

Usage.getUsersUsage();      // 获取所有用户的使用情况

贡献者

贡献者

示例代码

example/appwritex_example.dart

import 'package:dart_appwrite/dart_appwrite.dart';
import 'package:appwritex/appwritex.dart';

class MyService {
  MyService(
      {String endPoint = 'https://cloud.appwrite.io/v1',
      String project = '',
      String key = '',
      bool selfSigned = false}) {
    _apiClient.setEndpoint(endPoint).setProject(project).setKey(key);
    _publicClient.setEndpoint(endPoint);
  }

  final Client _apiClient = Client();
  final Client _publicClient = Client();

  late final Avatars _avatars = Avatars(_apiClient);
  late final Console _console = Console(_apiClient);
  late final Databases _databases = Databases(_apiClient);
  late final Functions _functions = Functions(_apiClient);
  late final Health _health = Health(_apiClient);
  late final Messaging _messaging = Messaging(_apiClient);
  late final Project _project = Project(_apiClient);
  late final Projects _projects = Projects(_apiClient);
  late final Public _public = Public(_publicClient);
  late final Proxy _proxy = Proxy(_apiClient);
  late final Storage _storage = Storage(_apiClient);
  late final Teams _teams = Teams(_apiClient);
  late final Users _users = Users(_apiClient);
  late final Vcs _vcs = Vcs(_apiClient);
  late final Usage _usage = Usage(_apiClient);

  Avatars get avatars => _avatars;
  Console get console => _console;
  Databases get databases => _databases;
  Functions get functions => _functions;
  Health get health => _health;
  Messaging get messaging => _messaging;
  Project get project => _project;
  Projects get projects => _projects;
  Proxy get proxy => _proxy;
  Public get public => _public;
  Storage get storage => _storage;
  Teams get teams => _teams;
  Users get users => _users;
  Vcs get vcs => _vcs;
  Usage get usage => _usage;
}

void main() async {
  final service = MyService(
    endPoint: 'https://cloud.appwrite.io/v1',
    project: '1234567890',
    key: '1234567890',
  );
  // 头像
  service.avatars
      .getCardsCloud(userId: '1234')
      .then((res) => print('Cards: $res'));
  service.avatars
      .getCardsCloudBack(userId: '1234')
      .then((res) => print('Cards Back: $res'));
  service.avatars
      .getCardsCloudOG(userId: '1234')
      .then((res) => print('Cards OG: $res'));

  // 控制台
  service.console
      .getVariables()
      .then((res) => print('Console Variables: $res'));

  // 数据库
  service.databases
      .getCollectionUsage(databaseId: '123', collectionId: '123')
      .then((res) => print('Collection Usage: $res'));
  service.databases
      .getDatabaseUsage(databaseId: '123')
      .then((res) => print('Database Usage: $res'));
  service.databases.getUsage().then((res) => print('Databases Usage: $res'));

  // 函数
  service.functions
      .getFunctionUsage(functionId: '123')
      .then((res) => print('Function Usage: $res'));
  service.functions.getUsage().then((res) => print('Functions Usage: $res'));

  // 健康
  service.health.getStats().then((res) => print('Health Stats: $res'));

  // 消息传递
  service.messaging.getUsage().then((res) => print('Messaging Usage: $res'));

  // 项目
  service.project
      .getVariables()
      .then((res) => print('Project Variables: $res'));
  service.project.getUsage().then((res) => print('Project Usage: $res'));

  // 项目列表
  service.projects.list().then((res) => print('Projects: $res'));

  // 代理
  service.proxy.listRules().then((res) => print('Proxy Rules: $res'));

  // 公共
  service.public.getVersions().then((res) => print('Public Versions: $res'));
  service.public
      .getHealthVersion()
      .then((res) => print('Public Health Version: $res'));

  // 存储
  service.storage
      .getBucketUsage(bucketId: '123')
      .then((res) => print('Bucket Usage: $res'));
  service.storage.getUsage().then((res) => print('Storage Usage: $res'));

  // 团队
  service.teams.getUsage().then((res) => print('Teams Usage: $res'));
  service.teams
      .listLogs(teamId: '123')
      .then((res) => print('Teams Logs: $res'));

  // 用户
  service.users.getUsage().then((res) => print('Users Usage: $res'));

  // VCS
  service.vcs
      .listInstallations()
      .then((res) => print('Vcs Installations: $res'));

  // 使用情况
  service.usage
      .getBucketUsage(bucketId: '123')
      .then((res) => print('Bucket Usage: $res'));
  service.usage
      .getCollectionUsage(databaseId: '123', collectionId: '123')
      .then((res) => print('Collection Usage: $res'));
  service.usage
      .getDatabaseUsage(databaseId: '123')
      .then((res) => print('Database Usage: $res'));
  service.usage
      .getFunctionUsage(functionId: '123')
      .then((res) => print('Function Usage: $res'));
  service.usage
      .getFunctionsUsage()
      .then((res) => print('Functions Usage: $res'));
  service.usage
      .getMessagingUsage()
      .then((res) => print('Messaging Usage: $res'));
  service.usage.getProjectUsage().then((res) => print('Project Usage: $res'));
  service.usage.getStorageUsage().then((res) => print('Storage Usage: $res'));
  service.usage.getTeamsUsage().then((res) => print('Teams Usage: $res'));
  service.usage.getUsersUsage().then((res) => print('Users Usage: $res'));
}

更多关于Flutter集成Appwrite插件appwritex的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter集成Appwrite插件appwritex的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter项目中集成并使用Appwrite插件(appwritex)通常涉及几个步骤,包括添加依赖、配置Appwrite SDK以及进行API调用。以下是一个基本的代码案例,展示了如何在Flutter项目中集成并使用Appwrite插件。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加appwritex依赖:

dependencies:
  flutter:
    sdk: flutter
  appwritex: ^版本号  # 请替换为实际的最新版本号

然后运行flutter pub get来安装依赖。

2. 配置Appwrite SDK

在你的Flutter项目中创建一个配置类或者直接在需要的地方配置Appwrite客户端。你需要提供Appwrite服务器的URL以及你的项目ID和API密钥。

import 'package:appwritex/appwritex.dart';

class AppwriteConfig {
  static Client getClient() {
    String endpoint = 'https://[YOUR_APPWRITE_ENDPOINT]/v1'; // 替换为你的Appwrite服务器URL
    String projectId = '[YOUR_PROJECT_ID]'; // 替换为你的项目ID
    String apiKey = '[YOUR_API_KEY]'; // 替换为你的API密钥

    Client client = Client();
    client.setEndpoint(endpoint);
    client.setProject(projectId);
    client.setKey(apiKey);

    return client;
  }
}

3. 使用Appwrite SDK进行API调用

下面是一个示例,展示了如何使用配置好的Appwrite客户端进行数据库文档的创建操作:

import 'package:flutter/material.dart';
import 'package:appwritex/appwritex.dart';
import 'appwrite_config.dart'; // 假设你将配置代码放在了一个单独的文件中

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Appwrite Flutter Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              // 获取客户端实例
              Client client = AppwriteConfig.getClient();

              // 准备数据库文档数据
              Map<String, dynamic> document = {
                'id': 'unique-document-id', // 文档ID,通常应保证唯一性
                'name': 'John Doe',
                'email': 'john.doe@example.com',
              };

              // 调用数据库创建文档API
              try {
                DatabaseCollectionIdResponse response = await client.database.createDocument(
                  collectionId: '[YOUR_COLLECTION_ID]', // 替换为你的集合ID
                  document: document,
                );

                // 处理响应
                print('Document created: ${response.data}');
              } catch (e) {
                // 处理错误
                print('Error: ${e.message}');
              }
            },
            child: Text('Create Database Document'),
          ),
        ),
      ),
    );
  }
}

注意事项

  1. 替换占位符:确保你替换了所有[YOUR_...]占位符为你的实际Appwrite服务器信息、项目ID、API密钥和集合ID。
  2. 错误处理:在实际应用中,你可能需要更详细的错误处理逻辑。
  3. 安全性:不要在客户端代码中硬编码API密钥。考虑使用环境变量或安全存储机制来保护敏感信息。

以上代码案例展示了如何在Flutter项目中集成并使用Appwrite插件进行基本的API调用。根据你的具体需求,你可以进一步扩展和定制这些代码。

回到顶部