Ad

How Can We Save The Data From A Complex Object In Firebase Firestore In Flutter?

- 1 answer

I use complex objects to manage data in my app. For example I have an object defined by the "Defi class" (meaning Challenge in French).

Here is the Defi class :

class Defi {
  final int modeApprentissage;
  final DateTime date;
  final double score;
  final int seconds;
  final List mots;
  final List erreurs;
  final List listes;
  final int langue;
  final int type;
  final int typeMots1;
  final int typeMots2;
  const Defi(
      {required this.modeApprentissage,
      required this.date,
      required this.type,
      required this.langue,
      required this.typeMots1,
      required this.typeMots2,
      required this.score,
      required this.seconds,
      required this.listes,
      required this.mots,
      required this.erreurs});
}

I have a LIST of Defi that I would like to save on FIREBASE FIRESTORE. My question : Is it absolutely necessary to transform my list of Defi into a map to save it on Firestore ? Or is there another way ?

Here is how I do it :

 List _defisMap = [];
    for (Defi def in _defis) {
      _defisMap.add({
        'modeApprentissage': def.modeApprentissage,
        'type': def.type,
        'langue': def.langue,
        'typeMots1': def.typeMots1,
        'typeMots2': def.typeMots2,
        'date': def.date,
        'score': def.score,
        'seconds': def.seconds,
        'listes': def.listes,
        'mots': def.mots,
        'erreurs': def.erreurs,
      });
    }
    if (await _connectivity.checkConnectivity() != ConnectivityResult.none) {
      await FirebaseFirestore.instance
          .collection('familyAccounts')
          .doc(_accountEmail)
          .update({
        'defis': _defisMap,
      });

I read in some article that in classes such as Defi, I could add a "factory" constructor ? Does this have anything to do with what I'd like to do ?

Thank you for your help :)

Ad

Answer

You need to create toJson method to set as a map of your list. If you have a list of Defi class. You can send it to map.

class Defi {
  final int modeApprentissage;
  final DateTime date;
  final double score;
  final int seconds;
  final List mots;
  final List erreurs;
  final List listes;
  final int langue;
  final int type;
  final int typeMots1;
  final int typeMots2;
  const Defi(
      {required this.modeApprentissage,
      required this.date,
      required this.type,
      required this.langue,
      required this.typeMots1,
      required this.typeMots2,
      required this.score,
      required this.seconds,
      required this.listes,
      required this.mots,
      required this.erreurs});

Map<String, dynamic> toJson() => {
       'modeApprentissage': modeApprentissage,
        'type': type,
        'langue': langue,
        'typeMots1': typeMots1,
        'typeMots2': typeMots2,
        'date': date,
        'score': score,
        'seconds': seconds,
        'listes': listes,
        'mots': mots,
        'erreurs': erreurs,
      };
}

Your list name is should be List<Defi> _defis then map it with toJson. var jsonMap = _defisMap.map((e) => e.toJson()).toList();


var jsonMap = _defis.map((e) => e.toJson()).toList();

if (await _connectivity.checkConnectivity() != ConnectivityResult.none) {
      await FirebaseFirestore.instance
          .collection('familyAccounts')
          .doc(_accountEmail)
          .update({
        'defis': jsonMap,
      });

Ad
source: stackoverflow.com
Ad