Adding an Object to Cloud Firestore using Flutter

后端 未结 3 1075
余生分开走
余生分开走 2021-01-02 06:44

I want to add an object to Google Cloud Firestore in my Flutter app like this:

\"Firestore

I ha

相关标签:
3条回答
  • 2021-01-02 07:05

    @Laksh22 As far as I understand, you mean something like this:

    Firestore.instance.runTransaction((transaction) async {
        await transaction.set(Firestore.instance.collection("your_collection").document(), {
            'reply' : {
            'replyName': replyName,
            'replyText': replyText,
            'replyVotes': replyVotes,
        }
    });
    

    just like the screenshot above.

    0 讨论(0)
  • 2021-01-02 07:09

    first, i highly recommend you have a single file that defines all of your schemas and/or models so there's a single point of reference for your db structure. like some file named dbSchema.dart:

    import 'package:meta/meta.dart';
    
    class Replies {
    
      final String title;  
      final Map coordinates;
    
      Replies({
        @required this.title,
        @required this.coordinates,
      });
    
     Map<String, dynamic> toJson() =>
      {
        'title': title,
        'coordinates': coordinates,
      };
    
    }
    

    and make the field that you want to be an object type Map. then, on the page you're going to insert into the db, import dbSchema.dart and create a new model:

    Replies _replyObj = new Replies(
      title: _topic,
      coordinates: _coordinates,
    );
    

    this assumes you've defined your local _coordinates (or whatever) object prior to this, with something like :

    _coordinates = {
     'lat': '40.0000',
     'lng': '110.000', 
    };
    

    and to then insert into Firestore, add the object's toJson method (you cannot insert/update a plain Dart model):

    CollectionReference dbReplies = Firestore.instance.collection('replies');
    
    Firestore.instance.runTransaction((Transaction tx) async {
      var _result = await dbReplies.add(_replyObj.toJson());
      ....
    

    Update (5/31)

    To convert the document read back into an object you need to add a fromJson to the class, like so:

    Replies.fromJson(Map parsedJson) {
        id = parsedJson['id']; // the doc ID, helpful to have
        title = parsedJson['title'] ?? '';
        coordinates = parsedJson['coordinates'] ?? {};
    }
    

    so when you query the db:

    QuerySnapshot _repliesQuery = await someCollection
            .where('title', isEqualTo: _title)
            .getDocuments();
    
    List<DocumentSnapshot> _replyDocs = _repliesQuery.documents;
    

    you can create an object out of each snapshot:

    for (int _i = 0; _i < _replyDocs.length; _i++) {
    
      Replies _reply = Replies.fromJson(_replyDocs[_i].data);
      _reply.id = _replyDocs[_i].documentID;
    
      // do something with the new _reply object
    }
    
    0 讨论(0)
  • 2021-01-02 07:11

    You can run a Firestore transaction like this:

        Firestore.instance.runTransaction((transaction) async {
              await transaction.set(Firestore.instance.collection("your_collection").document(), {
                'replyName': replyName,
                'replyText': replyText,
                'replyVotes': replyVotes,
              });
            });
    
    0 讨论(0)
提交回复
热议问题