Flutter构建工具插件candid_dart_build的使用

Flutter构建工具插件candid_dart_build的使用

pub,dev package publisher MIT

简体中文 | English

Dart Build System 提供处理 .did 文件的构建器,并快速生成与 .did 文件对应的 Dart 代码。

生成的代码包括:

  • IDL
  • Service 及其相关方法
  • .did 文件对应的对象及其相关方法

生成的代码依赖于 agent_dart

快速开始

依赖项简介

执行以下命令(推荐)

# 使用 Dart
dart pub add --dev candid_dart_build
dart pub add --dev build_runner

# 使用 Flutter
flutter pub add --dev candid_dart_build
flutter pub add --dev build_runner

手动添加到项目下的 pubspec.yaml

dev_dependencies:
  candid_dart_build: any
  build_runner: any

运行生成器

# 使用 Dart
dart run build_runner build

# 使用 Flutter
flutter pub run build_runner build

配置

targets:
  $default:
    builders:
      candid_dart_build|candid2dart:
        options:
          # 是否使用 Freezed,默认为 `false`
          freezed: false
          # 是否生成 `copyWith` 方法,默认为 `true`
          copy_with: true
          # 是否生成 `equals` 和 `hashCode` 方法,默认为 `true`
          equal: true
          # 是否使集合字段不可修改,仅在启用 Freezed 时有效,默认为 `true`
          make_collections_unmodifiable: true
          # 是否自动生成 `Service`?
          service: true
          # 导入设置包到每个生成的 Dart 文件。
          inject_packages:
            - package:recase/recase.dart
            - package:dart_style/dart_style.dart
          # 在调用 Actor 方法之前注入一段代码,可以引用请求参数 `request` 和类型为 CanisterActor 的参数 `actor`。
          pre_actor_call: |
            print(method);
            print(request);
            print(actor);
            // ...
          # 在调用 Actor 方法之后注入一段代码,可以引用请求参数 `request`、类型为 CanisterActor 的参数 `actor` 和方法的返回结果 `response`。
          post_actor_call: |
            print(method);
            print(request);
            print(actor);
            print(response);
            // ...

示例

请查看 示例 项目。

许可证

MIT License

Copyright (c) 2022 AstroxNetwork

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

示例代码

// example/lib/example.dart
void main(List<String> arguments) {}

更多关于Flutter构建工具插件candid_dart_build的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter构建工具插件candid_dart_build的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中集成和使用candid_dart_build插件的一个基本示例。请注意,candid_dart_build这个名称看起来是一个假定的插件名称,实际中可能并不存在这样的插件。但我会根据通常的Flutter插件集成步骤给出一个示例,你可以根据实际的插件文档进行调整。

步骤 1: 添加插件依赖

首先,你需要在pubspec.yaml文件中添加插件的依赖。由于candid_dart_build是假设的,我将使用一个假设的依赖名称candid_build_tool来演示。

dependencies:
  flutter:
    sdk: flutter
  candid_build_tool: ^0.1.0  # 假设的版本号

运行以下命令来获取依赖:

flutter pub get

步骤 2: 导入并使用插件

在你的Flutter项目中,你需要导入并使用这个插件。假设candid_build_tool插件提供了一个buildProject函数来执行构建任务。

// main.dart 或者其他你需要使用这个插件的文件
import 'package:flutter/material.dart';
import 'package:candid_build_tool/candid_build_tool.dart';  // 假设的导入路径

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Candid Build Tool Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              try {
                // 假设的 buildProject 函数调用
                await candidBuildTool.buildProject();
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Project built successfully!')),
                );
              } catch (e) {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Failed to build project: $e')),
                );
              }
            },
            child: Text('Build Project'),
          ),
        ),
      ),
    );
  }
}

步骤 3: 配置原生代码(如果需要)

某些插件可能需要在原生iOS和Android代码中进行配置。如果candid_build_tool有这样的要求,你应该查看插件的README文档来了解如何在ios/android/目录下进行配置。

注意事项

  1. 插件名称和函数candid_dart_buildcandid_build_tool都是假设的名称。你需要使用实际的插件名称和函数。
  2. 版本控制:在pubspec.yaml中指定一个合适的版本号。
  3. 文档:始终参考插件的官方文档,因为不同的插件可能有不同的集成步骤和API。

示例总结

这个示例演示了如何在Flutter项目中集成和使用一个假设的构建工具插件。请根据你的实际需求调整插件名称、导入路径和函数调用。如果你有一个具体的插件名称和文档,那么你应该按照那些文档中的步骤进行集成。

回到顶部