zemunkh
zemunkh

Reputation: 712

How to serialize/model inside model json data from Firestore on Flutter

How do I get access to the "HH" and "mm" using flutter data modeling technique? I've been trying to use it as follows, but it gives an error below.

enter image description here

My data model is currently a simplified version of it.

class Week {
  final String label;
  final String value;

  Week({@required this.label, @required this.value});

  factory Week.fromJson(Map<String, dynamic> doc) {
    return Week(
      label: doc['label'] ?? '',
      value: doc['value'] ?? 0,
    );
  }
}

class IntervalTime {
  final String hh;
  final String mm;

  IntervalTime({@required this.hh, @required this.mm});

  factory IntervalTime.fromJson(Map<String, dynamic> doc) {
    return IntervalTime(
      hh: doc['HH'] ?? '',
      mm: doc['mm'] ?? '',
    );
  }
}

class Diary {
  final String message;
  final List<Week> weeklyFreq;
  final Timestamp annualFreq;
  final IntervalTime start;

  Diary(
      {@required this.message,
      @required this.weeklyFreq,
      @required this.annualFreq,
      @required this.start});

  factory Diary.fromFirestore(DocumentSnapshot doc) {
    Map data = doc.data;
    return Diary(
      message: data['message'] ?? '',
      weeklyFreq: data['weeklyFreq'].cast<List<Week>>() ?? [],
      annualFreq: data['annualFreq'] ?? Timestamp,
      start: data['start'].cast<IntervalTime>() ?? '',
    );
  }
}

And logging is below.

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'CastList<dynamic, List<Week>>' is not a subtype of type 'List<Week>'

Upvotes: 1

Views: 633

Answers (1)

zemunkh
zemunkh

Reputation: 712

Workaround is below here. Only that matters most is to convert json data again in data declaration. Also, serializing data inside data object is a kind of confusing because there is enough resource to map object inside of object.

class Week {
  final String label;
  final int value;

  Week({@required this.label, @required this.value});

  factory Week.fromJson(Map<String, dynamic> data) {
    // Map<String, dynamic> data = json.decode(doc);
    return Week(
      label: data['label'] ?? '',
      value: data['value'] ?? 0,
    );
  }
}

class IntervalTime {
  final String hh;
  final String mm;

  IntervalTime({@required this.hh, @required this.mm});

  factory IntervalTime.fromJson(Map data) {
    return IntervalTime(
      hh: data['HH'] ?? '',
      mm: data['mm'] ?? '',
    );
  }
}

class Diary {
  final String message;
  final List<Week> weeklyFreq;
  final Timestamp annualFreq;
  final IntervalTime start;

  Diary(
      {@required this.message,
      @required this.weeklyFreq,
      @required this.annualFreq,
      @required this.start});

  factory Diary.fromFirestore(DocumentSnapshot doc) {
    Map data = doc.data;
    return Diary(
      message: data['message'] ?? '',
      weeklyFreq: (data['weeklyFreq'] as List)
          ?.map((e) => e == null ? null : Week.fromJson(e))
          ?.toList(), // Workaround
      annualFreq: data['annualFreq'] ?? Timestamp,
      start: IntervalTime.fromJson(data['start']), // workaround
    );
  }
}

Upvotes: 1

Related Questions