Skip to content

How to serialize in nullable type ? #1227

Answered by simolus3
lsaudon asked this question in Q&A
Discussion options

You must be logged in to vote

You could do something like this:

final listOfT = <T>[];
if (listOfT is List<DateTime?>) {
  return DateTime.parse(json as String) as T;
}
if (listOfT is List<Date?>) {
  return Date.fromJson(json as String) as T;
}

listOfT will be a List<Date?> if T is assignable to Date? which is probably what you want.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@lsaudon
Comment options

Answer selected by lsaudon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants