diff --git a/lib/data/model/container/image.dart b/lib/data/model/container/image.dart index d2b8e5e5..def66794 100644 --- a/lib/data/model/container/image.dart +++ b/lib/data/model/container/image.dart @@ -37,12 +37,12 @@ final class PodmanImg implements ContainerImg { String toRawJson() => json.encode(toJson()); factory PodmanImg.fromJson(Map json) => PodmanImg( - repository: json['repository'], - tag: json['tag'], - id: json['Id'], - created: json['Created'], - size: json['Size'], - containers: json['Containers'], + repository: _asString(json['repository']), + tag: _asString(json['tag']), + id: _asString(json['Id']), + created: _asInt(json['Created']), + size: _asInt(json['Size']), + containers: _asInt(json['Containers']), ); Map toJson() => { @@ -119,3 +119,16 @@ final class DockerImg implements ContainerImg { 'Tag': tag, }; } + +String? _asString(dynamic val) { + if (val == null) return null; + if (val is String) return val; + return val.toString(); +} + +int? _asInt(dynamic val) { + if (val == null) return null; + if (val is int) return val; + if (val is double) return val.toInt(); + return int.tryParse(val.toString()); +} diff --git a/lib/data/provider/container.dart b/lib/data/provider/container.dart index d4c1527b..3c6a4507 100644 --- a/lib/data/provider/container.dart +++ b/lib/data/provider/container.dart @@ -40,22 +40,15 @@ class ContainerNotifier extends _$ContainerNotifier { ContainerState build(SSHClient? client, String userName, String hostId, BuildContext context) { final type = Stores.container.getType(hostId); final initialState = ContainerState(type: type); - + // Async initialization Future.microtask(() => refresh()); - + return initialState; } Future setType(ContainerType type) async { - state = state.copyWith( - type: type, - error: null, - runLog: null, - items: null, - images: null, - version: null, - ); + state = state.copyWith(type: type, error: null, runLog: null, items: null, images: null, version: null); Stores.container.setType(type, hostId); sudoCompleter = Completer(); await refresh(); @@ -180,9 +173,13 @@ class ContainerNotifier extends _$ContainerNotifier { try { final statsLines = statsRaw.split('\n'); statsLines.removeWhere((element) => element.isEmpty); - for (var item in state.items!) { + final items = state.items; + if (items == null) return; + + for (var item in items) { final id = item.id; if (id == null) continue; + if (id.length < 5) continue; final statsLine = statsLines.firstWhereOrNull( /// Use 5 characters to match the container id, possibility of mismatch /// is very low. @@ -267,7 +264,6 @@ class ContainerNotifier extends _$ContainerNotifier { } } - const _jsonFmt = '--format "{{json .}}"'; enum ContainerCmdType {