Flutter插件功能介绍插件published的使用
Flutter插件功能介绍插件published的使用
动机
published
是受 Swift 中的 Published
属性包装器启发而创建的。
有许多方法如使用 BloC 需要依赖于流,这会引入大量样板代码。published
的目标是减少这些样板代码并引入简单的状态管理。
如何使用
安装
要在您的项目中使用 package:published
,请在 pubspec.yaml
文件中添加以下依赖项:
dependencies:
published:
dev_dependencies:
build_runner:
published_gen:
使用
首先,确保您已经安装了上述依赖项。然后,您可以按照以下步骤使用 published
插件:
import 'package:published/published.dart';
part 'counter_view_model.published.dart';
[@published](/user/published)
abstract class CounterViewModel extends _$CounterViewModel {
@Publisher()
abstract int count;
void increment() => count++;
[@override](/user/override)
bool get enableLogging => true;
}
- 您需要创建一个抽象类,并用
[@published](/user/published)
注解它,并扩展_$ClassName
。 - 使用
@Publisher()
注解抽象字段以暴露流。
注意:必须使用抽象字段和类来复制 Swift 结构。
接下来,运行以下命令生成文件到源目录中:
> flutter packages pub run build_runner build
[INFO] ensureBuildScript: Generating build script completed, took 368ms
[INFO] BuildDefinition: Reading cached asset graph completed, took 54ms
[INFO] BuildDefinition: Checking for updates since last build completed, took 663ms
[INFO] Build: Running build completed, took 10ms
[INFO] Build: Caching finalized dependency graph completed, took 44ms
[INFO] Build: Succeeded after 4687ms with 1 outputs
为了使用该模型,请执行以下操作:
// 初始化
final model = CounterViewModelBuilder.build(count: 0);
// 在构建函数中使用
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
StreamBuilder<int>(
stream: model.$count,
builder: (context, snapshot) => Text("${snapshot.data ?? 0}"),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => model.count++,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
$classNameBuilder.build()
需要用于创建对象实例。如果您不想在 build()
方法中传递默认值,可以使用 Publisher
上的 defaultValue
属性,如下所示:
@Publisher(defaultValue: "John")
abstract String name;
要观察任何带有 Publisher
注解的属性是否已更改,您可以使用 didChange
:
model.didChange.listen(() => doSomething());
如果您想启用日志记录,可以覆盖 enableLogging
获取器:
[@published](/user/published)
abstract class CounterViewModel extends _$CounterViewModel {
...
[@override](/user/override)
bool get enableLogging => true;
}
最后,如果您想在对象创建时执行某些操作,可以重写 onBind
方法并将逻辑放置在那里:
[@published](/user/published)
abstract class CounterViewModel extends _$CounterViewModel {
...
[@override](/user/override)
void onBind() {
super.onBind();
fetchSomeData();
}
}
更多关于Flutter插件功能介绍插件published的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter插件功能介绍插件published的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,published
通常是指将插件或包发布到 pub.dev 上,以便其他开发者可以轻松地将其集成到自己的项目中。pub.dev
是 Dart 和 Flutter 的官方包管理平台,类似于 npm 对于 Node.js 或 Maven 对于 Java。
1. 发布插件的步骤
要将 Flutter 插件发布到 pub.dev,你需要按照以下步骤操作:
1.1 创建插件
首先,确保你已经创建了一个 Flutter 插件。你可以使用以下命令来创建一个新的插件:
flutter create --template=package my_flutter_plugin
1.2 设置 pubspec.yaml
在 pubspec.yaml
文件中,确保你填写了所有必要的信息,如插件的名称、描述、版本号、依赖项等。以下是一个示例:
name: my_flutter_plugin
description: A sample Flutter plugin that demonstrates how to publish to pub.dev.
version: 0.0.1
author: Your Name <your.email@example.com>
homepage: https://www.example.com
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
1.3 检查代码质量
在发布之前,确保你的代码质量符合标准。你可以运行以下命令来检查:
flutter pub publish --dry-run
这将模拟发布过程并检查是否有任何问题。如果发现错误或警告,你需要先修复它们。
1.4 发布插件
如果一切准备就绪,你可以使用以下命令发布插件:
flutter pub publish
2. 插件发布后的管理
一旦插件成功发布到 pub.dev,你可以通过以下方式管理它:
- 更新版本:每次发布新版本时,记得更新
pubspec.yaml
中的版本号。 - 维护文档:确保你的插件有清晰的文档,包括如何使用它、API 参考等。
- 处理反馈:在 pub.dev 上,用户可以给你的插件评分和评论。你需要及时回复用户的反馈,并根据需要修复问题。
3. 插件的使用
其他开发者可以通过在 pubspec.yaml
中添加你的插件作为依赖项来使用它:
dependencies:
my_flutter_plugin: ^0.0.1