Skip to content

refactor(types): replace unsafe explicit type casting and JSON map casts with pattern-matching validation #1510

Open

Description

@ZhuchkaTriplesix

Summary

A static search across lib/ identified 245 unchecked explicit casts (as Map, as List, as String, etc.).

Most notably, in lib/core/extensions/local_extension_registry.dart:57:

final json = jsonDecode(content) as Map<String, dynamic>;

If a manifest file on disk contains a JSON array ([...]), a primitive value, or invalid formatting, this throws an unhandled TypeError at runtime (List<dynamic> is not a subtype of type Map<String, dynamic>).

Similarly, in lib/core/team/team_profile.dart (lines 129–137) and lib/core/erd/erd_saved_layout.dart (lines 172–181), values extracted from untyped maps are cast directly via item['field'] as String? without validating against corrupt database records.

Proposed Solution

  1. Use Dart 3 pattern-matching type guards for JSON deserialization:
    final decoded = jsonDecode(content);
    if (decoded is! Map<String, dynamic>) {
      throw FormatException('Expected JSON object, got ${decoded.runtimeType}');
    }
  2. Introduce a safe map extraction helper (e.g. asStringOrNull(), asIntOrNull()) for config/profile parsing.
  3. Replace raw as List<dynamic> casts with List<dynamic>.from(...) or typed collection checks.

Acceptance Criteria

  • Corrupted or unexpected JSON manifests / layout files produce clear diagnostic errors rather than unhandled TypeError crashes.
  • Safe type extraction helpers are used across extension and profile deserializers.

Activity

  1. added
    stabilityTheme parser epic label: stability
    coreCore library logic and services
    P3Low priority / Polish & Enhancements
    on Oct 12, 2026
  2. added this to the 0.5.0 milestone on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low priority / Polish & EnhancementscoreCore library logic and servicesstabilityTheme parser epic label: stability

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions