Flutter自定义Pod配置插件override_pod_annotation的使用

Flutter自定义Pod配置插件override_pod_annotation的使用

override_pod 插件是一个用于生成 Override 变量的工具,主要用于 Riverpod。该插件通过注解自动聚合定义的 Override 变量到一个数组中,并将其导出到生成的文件中。

概述

在覆盖 Riverpod 的 Provider 时,你需要在 ProviderScopeoverrides 属性中设置 Override 变量来覆盖 Provider。如果你需要覆盖多个 Provider,手动设置多个变量可能会变得繁琐,但使用 override_pod 可以简化这一过程。

覆盖前

import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
    runApp(
        ProviderScope(
            overrides: [
                fooProvider.overrideWith(FooProviderImpl.new),
                barProvider.overrideWith(BarProviderImpl.new),
                bazProvider.overrideWith(BazProviderImpl.new),
                quxProvider.overrideWith(QuxProviderImpl.new),
                quuxProvider.overrideWith(QuuxProviderImpl.new),
                corgeProvider.overrideWith(CorgeProviderImpl.new),
                graultProvider.overrideWith(GraultProviderImpl.new),
            ],
            child: const MyApp(),
        ),
    );
}

覆盖后

import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
    runApp(
        ProviderScope(
            overrides: packagePodOverrides,
            child: const MyApp(),
        ),
    );
}

这样可以大大简化代码。

开始使用

在你的 pubspec.yaml 文件中添加以下依赖:

dependencies:
  override_pod_annotation:

dev_dependencies:
  override_pod_generator:
  build_runner:

使用方法

  1. 使用注解定义变量。例如,在 foo/lib/src/repository/foo.dart 文件中:
import 'package:override_pod_annotation/override_pod_annotation.dart';

@overridePod
final pod = fooRepositoryProvider.overrideWith(FooRepositoryImpl.new);

class FooRepositoryImpl implements FooRepository {
    const FooRepositoryImpl(this.ref);
    final FooRepositoryRef ref;
}
  1. 使用 build_runner 生成代码:
$ flutter pub run build_runner build
  1. 导入生成的文件并使用生成的代码来覆盖 Pod。例如,在 lib/main.dart 文件中:
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:foo/override_pod.gen.dart';

void main() {
    runApp(
        ProviderScope(
            overrides: fooPodOverrides,
            child: const MyApp(),
        ),
    );
}

配置选项

你可以在 build.yaml 文件中配置选项。

targets:
  $default:
    builders:
      override_pod_generator:
        options:
          input_file: lib/repository/**.dart
          output_file: pods.gen.dart
          output_value_name: pods
          annotation_class: package:override_pod_generator/src/annotation.dart#Pods

input_file

  • 指定输入文件的路径。
  • 默认值为 lib/**.dart

output_file

  • 指定输出文件的路径。
  • 默认值为 lib/override_pod.gen.dart

output_value_name

  • 指定输出文件中的变量名。
  • 默认值为 ${package_name}PodOverrides

annotation_class

  • 指定注解类。
  • 默认值为 OverridePod

构建项目

如果要构建项目,可以运行以下命令:

$ make bootstrap

此外,你可以使用以下命令运行测试:

$ melos test

更多关于Flutter自定义Pod配置插件override_pod_annotation的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义Pod配置插件override_pod_annotation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在 Flutter 项目中,有时我们需要自定义 CocoaPods 的配置,例如修改 Podfile 中的某些配置项。override_pod_annotation 是一个用于在 Flutter 项目中覆盖或修改 Podfile 配置的插件。它允许你在 Flutter 项目中直接修改 Podfile 的配置,而无需手动编辑 Podfile 文件。

1. 安装 override_pod_annotation 插件

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

dependencies:
  flutter:
    sdk: flutter
  override_pod_annotation: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 使用 override_pod_annotation 插件

在 Flutter 项目的 ios 目录下,你可以使用 override_pod_annotation 插件来覆盖或修改 Podfile 的配置。以下是一个示例:

2.1 在 ios/Runner/GeneratedPluginRegistrant.m 文件中使用注解

你可以在 ios/Runner/GeneratedPluginRegistrant.m 文件中使用 override_pod_annotation 提供的注解来覆盖 Podfile 的配置。

#import <Flutter/Flutter.h>
#import "GeneratedPluginRegistrant.h"

// 使用 override_pod_annotation 提供的宏来覆盖 Podfile 配置
OVERRIDE_POD_CONFIGURATION({
    // 这里可以添加你想要覆盖的 Podfile 配置
    // 例如,修改某个 Pod 的版本
    pod 'Alamofire', '~> 5.4'
});

@implementation GeneratedPluginRegistrant

+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
  [GeneratedPluginRegistrant registerWithRegistry:registry];
}

@end

2.2 在 ios/Podfile 文件中使用注解

你也可以直接在 ios/Podfile 文件中使用 override_pod_annotation 提供的注解来覆盖或修改 Podfile 配置。

# ios/Podfile

platform :ios, '11.0'

# 使用 override_pod_annotation 提供的宏来覆盖 Podfile 配置
OVERRIDE_POD_CONFIGURATION({
    # 这里可以添加你想要覆盖的 Podfile 配置
    # 例如,修改某个 Pod 的版本
    pod 'Alamofire', '~> 5.4'
})

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  # Pods for Runner
  pod 'Flutter', :path => '.symlinks/flutter/ios'
  pod 'Firebase/Core', '~> 8.0'
end

3. 运行 pod install

在修改了 ios/Runner/GeneratedPluginRegistrant.mios/Podfile 文件后,你需要在 ios 目录下运行 pod install 来应用这些更改。

cd ios
pod install
回到顶部