4

I use a json file in a flutter app and recording this json in firestore fails.

my json is in jsonCloudContent. The following doesn't work :

Firestore.instance.collection('toto').document().setData(jsonCloudContent);

When I try with the string from json.encode(jsonCloudContent) it works fine :

Firestore.instance.collection('userStuffLists').document().setData({"apiVersion":2,"userId":"unknown","googleId":"unknown","list":[{"userI...

here's an example of the json file : https://jsoneditoronline.org/?id=3ac25ef1743047e08467cccbad031d5e

The function to upload data :

Future<bool> uploadUserStuffList() async {
  Map<String, dynamic> jsonCloudContent = StuffList(
    list: list,
    apiVersion: apiVersion,
    userId: userId,
    googleId: googleId,
    listVersion: listVersion)
    .toJson();
  try {
    Firestore.instance.collection('toto').document().setData(jsonCloudContent);
  } catch (e) {
    debugPrint(e);
  }
    return true;
  }

In Intellij inspector the format looks fine but just in case, here's the objects definition :

class Stuff {
  final String description;
  final List<String> imagePath;
  final String userId;
// TODO : put unknown in the key_strings ?
  Stuff({this.userId = 'unknown', this.description, this.imagePath});

  factory Stuff.fromJson(Map<String, dynamic> json) {
    return new Stuff(
      userId: json['userId'] != null ? json['userId'] : 'unknown',
//      TODO : throw error if description is null ? not certain
      description: json['description'],
      imagePath: json['path'] != null ? List<String>.from(json['path']) : null,
    );
  }

  Map<String, dynamic> toJson() => {
        'userId': userId != null ? userId : 'unknown',
        'description': description,
        'path': imagePath,
      };
}
class StuffList {
  final List<Stuff> list;
  final int apiVersion;
//  TODO need appropriate format when integrate firebase and google authentication
  final String userId;
  final String googleId;
  final int listVersion;
  StuffList({
    this.apiVersion = 2,
    this.userId = 'unknown',
    this.googleId,
    this.listVersion = 1,
    this.list,
  });

  factory StuffList.fromJson(Map<String, dynamic> json) {
    List<Stuff> listOfStuff = [];
    if (json['list'] != null) {
      json['list'].forEach((content) {
        Stuff stuff = Stuff.fromJson(content);
        listOfStuff.add(stuff);
      });
    } else {
      listOfStuff = null;
    }

    return new StuffList(
      apiVersion: json['apiVersion'],
      userId: json['userId'] != null ? json['userId'] : 'unknown',
      googleId: json['googleId'] != null ? json['googleId'] : 'unknown',
      listVersion: json['listVersion'],
//      list: json['list'] != null ? (json['list'] as List).map((i) => new Stuff.fromJson(i)) : null,
      list: listOfStuff,
    );
  }

  Map<String, dynamic> toJson() => {
        'apiVersion': apiVersion != null ? apiVersion : 1,
        'userId': userId != null ? userId : 'unknown',
        'googleId': googleId != null ? googleId : 'unknown',
        'listVersion': listVersion != null ? listVersion : 1,
        'list': list,
      };
}
5
  • Do you get any exceptions, errors, log outputs? Or does it just silently not write the map to firestore? Commented Feb 15, 2019 at 11:00
  • By record you mean Update the value in the database? if yes then you need to use the update method. Commented Feb 15, 2019 at 11:01
  • @Edman the try catch don't output any error Commented Feb 15, 2019 at 13:28
  • @SumitVairagar : I'm trying to do some insert. After I will try to deal with update. Commented Feb 15, 2019 at 13:29
  • I was not able to find the documentation for setData method, does anyone has any reference to that? Commented Feb 18, 2019 at 6:19

1 Answer 1

5

Found my error. The object StuffList contains a list of Stuff objects. The toJson method didn't convert list of Stuff to json. Here's the new method :

  Map<String, dynamic> toJson() => {
        'apiVersion': apiVersion != null ? apiVersion : 1,
        'userId': userId != null ? userId : 'unknown',
        'googleId': googleId != null ? googleId : 'unknown',
        'listVersion': listVersion != null ? listVersion : 1,
        'list': list.map((i) => i.toJson()).toList(),
      };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.