SidecarState enum

使用指南 0. 创建类与依赖注入 继承ProviderSidecar并实现方法; 添加注解, 运行 dart run build_runner watch 生成DI代码;

@lazySingleton
class DeviceNicknameProvider extends ProviderSidecar {
  // 0.1 `构造方法`与 `final属性`
  // 如`final ApiClient client;` 一般是从IoC中注入的基础设施, 如网络API, 本地存储等
  // 注意: DI会尝试注入构造函数的所有参数, 包括位置和可选参数

  // 0.2 配置核心`状态变量`或`get方法`
  // 如 `Process? proc;` 和与之对应的 bool get isRunning => proc!=null;

  // 0.3 配置 `setXxx`方法
  // 如`setUninitialized`表示设定内部状态,是瞬时的, 表示`设为..`
  // 此时状态通常是完成时, 如 `initialized`

  // **推荐使用**
  // 0.3.1 `reqWrapper` 的使用
  // 对于自定以的`setXxx`方法, 可以通过 `reqWrapper`装包业务逻辑

  // before:
  setStart() => setInitialized("正在启动..");

  // after:
  setStart() => reqWrapper(
        () => setInitialized("正在启动.."), // setXxx的简单业务逻辑
        accContain: [SidecarState.initialized], // 通过条件
        onReject: () => throw "尚未完成初始化, 当前状态[$state]",
      );

  // 0.4 配置 `actXxx`方法
  // 如`actInitializing`表示开始耗时方法,是耗时的, 表示`开始..`
  // 此时状态通常是进行时, 如 `initializing`

  // 0.4.1 `onInitializing`是`ProviderSidecar`提供的默认的act方法的包装
  // 覆写该方法后, 只需要调用 setXxx方法即可
  @override
  onInitializing() async {
    try {
    // ... logic
    setInitialized();
    } catch(e){
      if(...) setUninitialized();
      rethrow;
    }
  }

  // 0.4.2 自定义`actXxx`方法
  // 可以参照`ProviderSidecar`的`actInitializing`编写,
  // 注意返回值类型最好是 Exception类型, 便于Dialog或SnackBar处理抛出的异常

  // **推荐使用**
  // 0.4.2.1 `actWrapper` 的使用
  // 对于自定义的 `actXxx`方法, 可以使用 actWrapper 对业务逻辑进行包装,
  // 以自动实现对 异常的 try..catch处理

  // before:
  Future<EX?> actRun() async {
  // 方法体内部需要自行处理try..catch,否则无法捕获和处理抛出的异常
    // 进入方法后立即切换 state
    setStart();
    // ... 业务逻辑 ... 包含异常的抛出
  }

  // after:
  Future<EX?> actRun() => actWrapper(() async {
    // 进入方法后立即切换 state
    setStart();
    // ... 业务逻辑 ... 包含异常的抛出
  });
  1. Provider注册 即注册到MultiProvider
  • 如果Provider使用默认的初始化值(构造方法), 并且没有notify的需求,则无需调用setUninitialized方法
  • 推荐使用构造方法进行初始化
 ChangeNotifierProvider(
     create: (c) => sl<DeviceNicknameProvider>()..setUninitialized()),
 )
  1. 使用
Consumer<DeviceNicknameProvider>(
  builder: (_, p, __) {
  // ...
  }
)
  1. <可选> 指定抛出异常类型 BaseException 可以是App内异常的基类, 以便UI统一处理(弹出Dialog,Toast等)
class MySidecar extends ProviderSidecar<BaseException>{
  @override
  onInitializing() {
    // ...
  }
}

BaseException 示例 (来自 get_arch_core package)

class BaseException {
  final String msg; // 直接向用户展示
  final String debugInfo; // 用于问题反馈,帮助定位BUG的信息,可以用stacktrace填充

  BaseException(this.msg, {this.debugInfo = ''});

  @override
  String toString({int maxInfoLen = 100}) =>
      'BaseException{msg: $msg, debugInfo: ${(debugInfo.length > maxInfoLen) ? '${debugInfo.substring(0, maxInfoLen)}...' : debugInfo}}';
}

状态

Inheritance

Constructors

SidecarState()
const

Values

uninitialized → const SidecarState
initializing → const SidecarState
initialized → const SidecarState

Properties

hashCode int
The hash code for this object.
no setterinherited
index int
A numeric identifier for the enumerated value.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Constants

values → const List<SidecarState>
A constant List of the values in this enum, in order of their declaration.