Skip to content

Type int is not a subtype of type String? in type cast #37

@alboiuvlad29

Description

@alboiuvlad29

This happens when we're running a flutter app on iOS with cx_flutter_plugin. It looks like response_content_length is being sent as a String from Android native side while on iOS, it is being sent as an int while trying to parse NetworkRequestContext.

We forked the package and are using the following hot-fix in the meantime, though it should probably be fixed on the platform channel level.

 factory NetworkRequestContext.fromJson(Map<String, dynamic> json) {
    return NetworkRequestContext(
      method: json['method'] as String,
      statusCode:
          json['status_code'] is String ? int.tryParse(json['status_code'] as String) ?? 0 : json['status_code'] as int,
      url: json['url'] as String,
      fragments: json['fragments'] as String?,
      host: json['host'] as String?,
      schema: json['schema'] as String?,
      statusText: json['status_text'] as String?,
-     responseContentLength: json['response_content_length'] as String?,
+     responseContentLength: switch (json['response_content_length']) {
+       String s => s,
+       int i => i.toString(),
+       null => null,
+         _ => '0'
+     },
      duration:
          json['duration'] is String ? int.tryParse(json['duration'] as String) ?? 0 : json['duration'] as int? ?? 0,
    );
  }
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions