在扑扑中从Firestore载入阵列

时间:2019-05-31 17:30:26

标签: flutter google-cloud-firestore

在Firestore中,我有一个结构如下的文档: 名称:字符串, 姓:字符串, 生日:字符串, phoneNumber:字符串, r:列表,

其中MyObject自身包含一个列表,但是当我检索文档并将其映射为对象(MyMainObject)时,它将引发未处理的异常,该异常指出:类型'_InternalLinkedHashMap'不是类型'Map'的子类型

在添加列表之前,一切正常,并且已正确检索对象,但是在添加列表之后,它将引发此异常

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class MyMainObject {
  String name;
  String surname;
  String birthday;
  String phoneNumber;
  int id;
  List<MyObject> r;

  MyMainObject(
      {this.name,
      this.surname,
      this.birthday,
      this.phoneNumber,
      this.id,
      this.r});

  factory MyMainObject.fromSnapshot(Map<String, dynamic> parsedSnap) {
    var parsedList = parsedSnap['r'] as List;
    List<MyObject> rList = parsedList.map((i) {
      MyObject.fromJson(i);
    }).toList();
    return MyMainObject(
      name: parsedSnap['name'] as String,
      surname: parsedSnap['surname'] as String,
      birthday: parsedSnap['birth'] as String,
      phoneNumber: parsedSnap['phoneNumber'] as String,
      id: parsedSnap['id'] as int,
      r: rList,
    );
  }
}

class MyObject {
  String dateTime;
  String spec;
  List<String> laps; //Before adding this everything was ok
  String totalLap;

  MyObject({this.dateTime, this.spec});

  factory MyObject.fromJson(Map<String, dynamic> parsedJson) {
    String _totalTempLap = parsedJson['totalLap'];
    List<String> _lapsTemp = new List<String>.from(parsedJson['laps']);
    MyObject r =
        Run(dateTime: parsedJson['dateTime'], spec: parsedJson['spec']);
    r.setLaps(_lapsTemp);
    r.setTotalLap(_totalTempLap);
    return r;
  }
}

class RetrieveObject extends StatefulWidget {
  RetrieveObjectState createState() => new RetrieveObjectState();
}

class RetrieveObjectState extends State<RetrieveObject> {
  MyMainObject myMainObject;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    Firestore.instance
      ..collection('/maincollection')
          .document('maindocument')
          .collection('mainObjectCollection')
          .document['mainObjectName']
          .snapshots()
          .forEach((doc) {
        setState(() {
          myMainObject = MyMainObject.fromSnapshot(doc.data);
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return Text('${myMainObject.name}');
  }
}

我希望有myMainObject,但最终我遇到了未处理的异常。

0 个答案:

没有答案