ModelSettings.fromJson constructor

ModelSettings.fromJson(
  1. Map<String, Object?> json
)

Restores settings written by toJson. Entries that cannot be read are skipped rather than failing the whole settings.

Implementation

factory ModelSettings.fromJson(Map<String, Object?> json) {
  final providers = <ProviderConfig>[];
  final entries = json['providers'];
  if (entries is List) {
    for (final entry in entries) {
      if (entry is! Map) continue;
      try {
        providers.add(
          ProviderConfig.fromJson(Map<String, Object?>.from(entry)),
        );
      } on FormatException {
        continue;
      }
    }
  }
  final selected = json['selectedProviderId'];
  return ModelSettings(
    useThirdParty: json['useThirdParty'] == true,
    providers: List.unmodifiable(providers),
    selectedProviderId: selected is String ? selected : null,
  );
}