Home > OS >  Firestore adds the same data every time I restart the device or emulator in a flutter app
Firestore adds the same data every time I restart the device or emulator in a flutter app

Time:06-16

I get a json data from an external API. I want to manage this data on Firebase Firestore database to manage it easily like sorting etc... And then I build a sorted list by using a listview builder. I put firestore initialization and add() function in initState() to add the json data to firestore database before building the list. Everytime I restart the app on an emulator or a real device, firestore add() function works as well and adds the same data again of course. What is the best practice of using the firestore initialization. Here is my initState(). Probably I should not initialize firestore in initState().

@override
  void initState() {
    super.initState();

    *//I get the json data with ordersTodayResponse function.*

    ordersTodayResponse.then((value) {
      snapshotToday = value;
      orderListToday = snapshotToday!.orders;

    *//I make an order list from the snapshot* 

      modelToList(List<OrdersModel> orderList) {
        List<Map<String, dynamic>> orders = [];
        for (var orderModel in orderList) {
          Map<String, dynamic> order = orderModel.toMap();
          orders.add(order);
        }
        return orders;
      }
     
     *//I get the each order to add as a document to firestore.*

      final orderList = modelToList(orderListToday);
      for (var order in orderList) {
        FirebaseServices.firestoreDatabase.collection('orders').add(order);
    });
  }

CodePudding user response:

If you are overriding the data each time use .set rather than .add, or clear the documents before you add.

For whether or not you should put it in the initState, not that bad idea unless the first page of your app that opens relies on the data. If it does, would recommend putting it in an async Future function, then surround your main page with a FutureBuilder.

An alternative to using firestore could be using a sqflite db or just creating a local file to store a json.

CodePudding user response:

I agree with shed's answer. In this scenario, I think it's fine to initialize your data this way.

To avoid adding the same data every restart. You may try this:

@override
      void initState() {
        super.initState();
          //you could reset your list first
          orderListToday.clear();
    
          //then, proceed...
          ordersTodayResponse.then((value) {
              snapshotToday = value;
              orderListToday = snapshotToday!.orders;
    
              final orderList = modelToList(orderListToday);
              for (var order in orderList) {
                //use .set instead
                FirebaseServices.firestoreDatabase.collection('orders').set(order);
              }
            );
          }
      }
        
    
      //you should put this method outside, it can still be called inside initState
      modelToList(List<OrdersModel> orderList) {
        List<Map<String, dynamic>> orders = [];
        for (var orderModel in orderList) {
          Map<String, dynamic> order = orderModel.toMap();
          orders.add(order);
        }
        return orders;
      }
  • Related