Problem retrieving firebase lists using mobx

Asked

Viewed 22 times

1

Whereas I have a product with several items/complements loaded from firebase..

Each of these items has a checkbox to check whether the user will add this item to the order or not. And, state control is done using mobx to redo only the item that was changed.

However, with an add-on list works perfectly, the problem is when I have more than one add-on (with several items) mobx loses the reference of which list is being referred and ends up giving bug when selecting an item from the list.

List of complements:

recuperarComplementos(){
    //print("chamou recuperar complementos");

    return StreamBuilder(
      stream: FirebaseFirestore.instance
        .collection("complementos")
        .doc(widget.produto.idProduto)
        .collection("complementos")
        .orderBy("posicao")
        .snapshots(),
      builder: (context, snapshot){

        switch(snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return carregandoDados;
            break;
          case ConnectionState.active:
          case ConnectionState.done:
            QuerySnapshot querySnapshot = snapshot.data;

            if(querySnapshot.docs.length == 0){
              return Container(
                padding: EdgeInsets.all(25),
                child: Center(
                  child: Text("Nenhum complemento!",
                    style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold
                    ),),
                ),
              );
            }

            return Container(
              width: double.infinity,
              child: ListView.builder(
                //reverse: true,
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                physics: ScrollPhysics(),
                itemCount: querySnapshot.docs.length,
                itemBuilder: (context, indice){

                  List<DocumentSnapshot> complementos = querySnapshot.docs.toList();
                  DocumentSnapshot documentSnapshot = complementos[indice];
                  Complemento complemento = Complemento.fromDocumentSnapshot(documentSnapshot);

                  return Column(
                    children: [
                      listaItensComplemento(complemento),
                    ],
                  );

                },
              ),
            );

        }
        return Container();
      },
    );

  }

List of complement items:

listaItensComplemento(Complemento complemento){
   
    return StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection("complementos")
          .doc(widget.produto.idProduto)
          .collection("complementos")
          .doc(complemento.idComplemento)
          .collection("itens")
          .orderBy("posicao", descending: false)
          //.limit(3)
          .snapshots(),
      builder: (context, snapshot){

        switch(snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return carregandoDados;
            break;
          case ConnectionState.active:
          case ConnectionState.done:
            QuerySnapshot querySnapshot = snapshot.data;

            if(querySnapshot.docs.length == 0){
              return Container(
                padding: EdgeInsets.all(25),
                child: Center(
                  child: Text("Nenhum item disponível!",
                    style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold
                    ),),
                ),
              );
            }

            return Container(
              width: double.infinity,
              child: ListView.builder(
                //reverse: true,
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                physics: ScrollPhysics(),
                itemCount: querySnapshot.docs.length,
                itemBuilder: (context, indice){
                  //print(indice);
                  List<DocumentSnapshot> complementos =
                    querySnapshot.docs.toList();
                  DocumentSnapshot documentSnapshot = complementos[indice];
                  final ItemComplemento itemComplemento =
                    ItemComplemento.fromDocumentSnapshot(documentSnapshot);

                  Map<String, dynamic> novoItem = {
                    "marcou": false,
                    "titulo": 5,
                  };
                  itemComplemento.marcado = false;
                  itemComplementoModel.addItem(novoItem);

                  if(indice == 0){
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        CardComplemento(complemento),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            PartComplemento(
                              indice: indice,
                              itemComplemento: itemComplemento,
                              carrinhoModel: carrinhoModel,
                              itemComplementoModel: itemComplementoModel,
                              complemento: complemento,
                            ),
                            Container(
                                margin: EdgeInsets.only(left: 16, right: 16),
                                child: Divider(color: Colors.grey[300],)),
                          ],
                        ),
                      ],
                    );

                  }

                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      PartComplemento(
                        indice: indice,
                        itemComplemento: itemComplemento,
                        carrinhoModel: carrinhoModel,
                        itemComplementoModel: itemComplementoModel,
                        complemento: complemento
                      ),
                      Container(
                          margin: EdgeInsets.only(left: 16, right: 16),
                          child: Divider(color: Colors.grey[300],)),
                    ],
                  );
                },
              ),
            );

        }
        return Container();
      },
    );
  }

Complement Model Item (mobx):

abstract class _ItemComplementoModel with Store{

  @observable
  ObservableList<Map<String, dynamic>> listaItens = ObservableList<Map<String, dynamic>>();

  @observable
  int qtdSelecionada = 0;

  @action
  void addItem(Map<String, dynamic> novoItem){
    listaItens.add(novoItem);
  }

  @action
  void marcarItem(
      {int indice,
      bool marcou,
      CarrinhoModel carrinhoModel,
      ItemComplemento itemComplemento,
      Complemento complemento
      }){

      if(marcou == true){
        listaItens[indice] = {"marcou": true};
        qtdSelecionada++;
      }else{
        listaItens[indice] = {"marcou": false};
        qtdSelecionada--;
      }

  }


}

My question is, how could I add a list inside the mobx for each add-on automatically using firebase, so yes mobx know the reference of each add-on and its respective items..

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.