fromMap static method

TaskStatus fromMap(
  1. Map<String, dynamic> map
)

Deserializes from an event map (from platform EventChannel).

Implementation

static TaskStatus fromMap(Map<String, dynamic> map) {
  final type = map['type'] as String?;
  final executionId = map['executionId'] as String? ?? '';
  final taskName = map['taskName'] as String? ?? '';

  Map<String, dynamic>? castData(dynamic data) {
    if (data is Map) {
      return Map<String, dynamic>.from(data.cast<String, dynamic>());
    }
    return null;
  }

  return switch (type) {
    'queued' => TaskQueued(
        executionId: executionId,
        taskName: taskName,
      ),
    'running' => TaskRunning(
        executionId: executionId,
        taskName: taskName,
        progress: (map['progress'] as num?)?.toDouble() ?? 0.0,
      ),
    'succeeded' => TaskSucceeded(
        executionId: executionId,
        taskName: taskName,
        data: castData(map['data']),
      ),
    'failed' => TaskFailed(
        executionId: executionId,
        taskName: taskName,
        error: map['error'] as String?,
        attempt: map['attempt'] as int? ?? 0,
        maxAttempts: map['maxAttempts'] as int? ?? 0,
      ),
    'retrying' => TaskRetrying(
        executionId: executionId,
        taskName: taskName,
        attempt: map['attempt'] as int? ?? 0,
        nextAttempt: map['nextAttempt'] != null
            ? DateTime.parse(map['nextAttempt'] as String)
            : DateTime.now(),
      ),
    'cancelled' => TaskCancelled(
        executionId: executionId,
        taskName: taskName,
      ),
    _ => TaskQueued(
        executionId: executionId,
        taskName: taskName,
      ),
  };
}