0

我在使用flutter将数据保存在firestore数据库中时遇到问题,我有这两个类:

class Level {
  final String name;
  final List<Materia> materie;
  Level({
    this.name,
    this.materie,
  });
  Map toJson() {
     return {"name": name, "materie": materie};
  }
}
class Materia extends Taggable {
  final String name;
  Materia({
    this.name,
  });
  @override
  List<Object> get props => [name];
}

我想将其保存在 firestore 的 MAP 中,但收到此错误:

Unhandled Exception: Invalid argument: Instance of 'Level'

这是我用来保存数据的代码:

Future<bool> saveProfile({String uid, String name, String surname, String address, List<Level> level}) async {
    try {
      Firestore.instance.collection('users').document(uid).updateData({ 'name': name, 'surname': surname, 'address': address, 'level': level.toJson() });
      return true;
    }  catch (e) {
      // throw the Firebase AuthException that we caught
      throw new Exception(e.toString());
    }
  }

谢谢!!

4

1 回答 1

1

您需要在 Level 类中创建一个映射器函数,如下所示:

Map toJson(){
  return {
    "name" : name,
    "materie" : materie
  };
}

然后像这样更新您的 Firebase 更新代码: Firestore.instance.collection('users').document(uid).updateData({ 'name': name, 'surname': surname, 'address': address, 'level': level.toJson() });

于 2020-01-11T14:25:52.280 回答