Flutter学习资料生成插件carp_study_generator的使用

Flutter学习资料生成插件carp_study_generator的使用

CARP Study Generator Utility Package

该实用包帮助生成用于CARP移动感知研究所需的配置文件,并将这些文件上传到CARP Web服务器。

配置和设置

要使用研究生成器,请在您的应用程序中执行以下步骤:

  1. pubspec.yaml文件中将carp_study_generatortest作为dev_dependencies包含进来。
  2. 将文件夹carp复制到项目的根目录。
  3. 配置carpspec.yaml文件,以及protocol.jsonconsent.json文件,以及消息和语言的JSON文件(如en.json等)。

carpspec.yaml的配置

carpspec.yaml文件可以使用以下属性进行配置:

  • CARP服务器
  • 研究ID
  • 协议
  • 知情同意书
  • 消息
  • 语言本地化
server:
  uri: https://cans.cachet.dk
  client_id: carp
  client_secret: carp
  username: user@dtu.dk
  password: pw

# 基本研究ID
study:
  study_id: 01cf04a7-d154-40f0-9a75-ab759cf74eb3

# 协议文件的路径
protocol:
  path: carp/resources/protocol.json

# 知情同意书文件的路径
consent:
  path: carp/resources/consent.json

# 消息配置
message:
  # 消息文件的路径
  path: carp/messages/
  # 列出需要上传的消息
  # 在[path]文件夹中添加每个消息为<name>.json文件
  messages: 
    - 1
    - 2

localization:
  path: carp/lang/
  # 列出支持的语言
  # 对于每种语言,在'lang'文件夹中必须添加一个json文件
  locales:
    - en
    - da

请注意,carpspec.yaml文件包含明文用户名和密码,因此不应将其添加到版本控制——请将其添加到.gitignore文件中。

文件结构

所有用于创建和上传配置到CARP的文件都存储在项目根目录下的carp文件夹中。需要上传的JSON文件名在carpspec.yaml文件中指定(见上文)。默认的文件结构如下:

文件 描述
resources/protocol.json 您的SmartphoneStudyProtocol的JSON定义。
resources/consent.json 您的RPOrderedTask的JSON定义,其中包含要向用户显示的知情同意书。
lang/<language>.json 每种受支持语言的JSON语言文件,形式为<language>.json
messages/<name>.json 需要上传的每个JSON消息文件的名称。

请忽略carp文件夹中的测试脚本(这些用于执行命令)。

使用方法

每个命令的运行方式如下:

flutter test carp/<command>

可用的命令如下:

  help                   打印此帮助信息。
  dry-run                 进行干运行测试,检查对CARP服务器的访问权限及JSON资源的正确性。
  create                 基于JSON文件创建研究协议并上传到CARP服务器。
  update                 更新现有研究协议为新版本。
  consent                基于JSON文件创建知情同意书并上传到CARP服务器。
  localization           上传本地化文件到CARP服务器。
  message                上传消息列表到CARP服务器。
  message-delete-all     删除CARP服务器上的所有消息。

在上传任何JSON文件到CARP之前,请先运行dry-run命令。它会检查并输出如下列表:

[✓] CARP App             CarpApp - name: CARP服务器 'https://cans.cachet.dk/dev', uri: https://cans.cachet.dk/dev, studyDeploymentId: null, studyId: 33441683-bbec-4c85-95de-b27aec09afce
[!] CARP Server          CarpServiceException: 401 Unauthorized -  完整身份验证是访问此资源所必需的 -
[✓] Protocol path        carp/resources/protocol.json
[✓] Protocol parse       name: CAMS App Study No. 2
[✓] Consent path         carp/resources/consent.json
[✓] Consent              identifier: consentTaskID
[✓] Locale - en          carp/lang/en.json
[✓] Locale - da          carp/lang/da.json
[✓] Message - 1          carp/messages/1.json
[✓] Message - 2          carp/messages/2.json
 •  1 issues found!

更多关于Flutter学习资料生成插件carp_study_generator的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter学习资料生成插件carp_study_generator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


carp_study_generator 是一个用于生成 Flutter 学习资料的插件。它可以帮助开发者快速生成 Flutter 项目的学习资料,包括代码示例、文档、测试用例等。以下是如何使用 carp_study_generator 的基本步骤:

1. 安装插件

首先,你需要在 pubspec.yaml 文件中添加 carp_study_generator 插件的依赖项:

dependencies:
  flutter:
    sdk: flutter
  carp_study_generator: ^latest_version

dev_dependencies:
  build_runner: ^latest_version

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

2. 创建学习资料模型

在你的 Flutter 项目中,创建一个 Dart 文件来定义学习资料的数据模型。例如:

import 'package:carp_study_generator/carp_study_generator.dart';

part 'study_model.g.dart';

@StudyModel()
class Study {
  final String title;
  final String description;
  final List<Example> examples;

  Study({
    required this.title,
    required this.description,
    required this.examples,
  });
}

@ExampleModel()
class Example {
  final String code;
  final String description;

  Example({
    required this.code,
    required this.description,
  });
}

3. 生成学习资料

运行以下命令来生成学习资料:

flutter pub run build_runner build

这将会在 study_model.g.dart 文件中生成相应的代码。

4. 使用生成的学习资料

你可以在你的 Flutter 项目中使用生成的学习资料。例如:

import 'study_model.dart';

void main() {
  final study = Study(
    title: 'Flutter Basics',
    description: 'Learn the basics of Flutter development.',
    examples: [
      Example(
        code: 'print("Hello, Flutter!")',
        description: 'A simple Flutter example.',
      ),
    ],
  );

  print(study.title);
  print(study.description);
  for (var example in study.examples) {
    print(example.code);
    print(example.description);
  }
}

5. 自定义生成内容

你可以通过注解和配置来自定义生成的学习资料内容。例如,你可以使用 @ExampleModel 注解来定义不同的示例类型,或者使用 @StudyModel 注解来定义不同的学习主题。

6. 更新学习资料

如果你修改了数据模型,记得重新运行 flutter pub run build_runner build 来更新生成的学习资料。

7. 清理生成的文件

如果你想清理生成的文件,可以运行以下命令:

flutter pub run build_runner clean
回到顶部