How do I resolve type 'Timestamp' is not a subtype of type 'String' in type cast

Reed

I want to fetch meetings from Firestore and map them into the following Meeting model:

part 'meeting.g.dart';

@JsonSerializable(explicitToJson: true)
class Meeting {
  String id;
  DateTime date;

  Meeting(this.id, this.date);

  factory Meeting.fromJson(Map<String, dynamic> json) {

    return _$MeetingFromJson(json);
  }

  Map<String, dynamic> toJson() => _$MeetingToJson(this);
}

The documents are fetched from Firestore and then fromJson is called on the iterable, but an exception is thrown:

type 'Timestamp' is not a subtype of type 'String' in type cast

When I go into generated meeting.g.dart, it's this line which causes the error

json['date'] == null ? null : DateTime.parse(json['date'] as String)

To workaround the issue, I've tried changing from DateTime to Timestamp in the model, but then the following build error is shown:

Error running JsonSerializableGenerator
Could not generate `fromJson` code for `date`.
None of the provided `TypeHelper` instances support the defined type.

Could you tell me how do you solve this issue? Is there another preferred way to combine Firebase and a Flutter project using json_serializable for JSON serialization? Maybe even replace usage of json_serializable ?

Reed

Solution #1

Use toJson and fromJson converter functions as in the following example: https://github.com/dart-lang/json_serializable/blob/master/example/lib/example.dart

Benefits of the solution is that you don't have to hard-code property names

Solution #2

After reading https://github.com/dart-lang/json_serializable/issues/351, I've changed Meeting.fromJson and it works as expected now:

  factory Meeting.fromJson(Map<String, dynamic> json) {
    json["date"] = ((json["date"] as Timestamp).toDate().toString());
    return _$MeetingFromJson(json);
  }

json["date"] is Timestamp by default, I convert it to String, before it reaches the generated deserializer, so it doesn't crash when it tries to cast json["date"] as String

Though, I don't like this workaround very much, because I have to hard-code property's name and couple to types, but for now, this solution will be good enough.

An alternative would be to try out https://pub.dev/packages/built_value for serialiazion, which is recommended in their blog https://flutter.dev/docs/development/data-and-backend/json

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to resolve - type 'Null' is not a subtype of type 'String' in type cast?

How do I solve this case of "type 'String' is not a subtype of type 'int'" and type 'int' is not a subtype of type 'String'

How to solve error "type 'Null' is not a subtype of type 'String' in type cast"

How do i solve the error : type 'Null' is not a subtype of type 'String'

type 'CategoriesScreen' is not a subtype of type 'String' in type cast

How to fix _castError List<dynamic>' is not a subtype of type 'String' in type cast

How to resolve "Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<DocumentSnapshot>' in type cast"?

How do I get the String type or timestamp type by giving a year

type 'Future<String>' is not a subtype of type 'String' in type cast in flutter

type 'List<DropdownMenuItem<String>>' is not a subtype of type 'List<String>?' in type cast

i got this error "Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast" as below

InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Timestamp' in type cast

How to fix type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast error

Flutter: _CastError (type 'Null' is not a subtype of type 'String' in type cast)

type 'String' is not a subtype of type 'Route<Object?>' in type cast

type 'bool' is not a subtype of type 'List<String>' in type cast - flutter/dart

Error: type 'String' is not a subtype of type 'List<dynamic>' in type cast

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Client' in type cast in flutter?

Type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast error

Flutter error: type 'int' is not a subtype of type 'String' in type cast

_CastError (type 'String' is not a subtype of type 'DateTime' in type cast)

Flutter:type 'Null' is not a subtype of type 'String' in type cast

Flutter error: "The type 'null' is not a subtype of type 'String' in type cast"

flutter Mapping error: type 'String' is not a subtype of type 'Widget' in type cast

Flutter Error: type 'AddressInfo' is not a subtype of type 'String' in type cast

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'TodoModel' in type cast

Unhandled Exception: type 'String' is not a subtype of type 'num?' in type cast

Exception: type 'String' is not a subtype of type 'int' in type cast

type 'int' is not a subtype of type 'String' in type cast json | decode Problem