简体   繁体   中英

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.

在此处输入图像描述

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>'

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
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM