skip to Main Content

I’d Like to get data in home screen of my flutter app, where I have list of OfferCards, these are generated from firestore via FirestoreQueryBuilder in my homeView like this

FirestoreQueryBuilder<OfferData>(
          pageSize: 10,
          query: FirebaseFirestore.instance
              .collection('Offers')
              .orderBy('CreatedAt', descending: true)
              .withConverter<OfferData>(
                  fromFirestore: ((snapshot, options) =>
                      OfferData.fromJson(snapshot.data()!)),
                  toFirestore: (value, options) => value.toJson()),
          builder: (context, snapshot, _) {
            if (snapshot.isFetching) {
              return const Center(
                child: CircularProgressIndicator(color: Colors.greenAccent),
              );
            } else if (snapshot.hasError) {
              return const Center(
                child: Text('Server error'),
              );
            } else if (snapshot.docs.isEmpty) {
              return const Center(
                child: Text('No offers'),
              );
            } else {
              return ListView.builder(
                  itemBuilder: (context, index) {
                    final hasReachEnd = snapshot.hasMore &&
                        index + 1 == snapshot.docs.length &&
                        !snapshot.isFetchingMore;

                    if (hasReachEnd) {
                      snapshot.fetchMore();
                    }
                    final post = snapshot.docs[index].data();
                    homeController.offers[index] = post;
                    return OfferCardView();
                  },
                  itemCount: snapshot.docs.length);
            }
          },
        )

As on the end of this example, inside HomeController I have Map of int and UserData, which is filled with all offers. Each offerCardView has Get.find to HomeController to have access to this map. And here’s my question, how do I determine inside of OfferCardView and later in OfferView(after tapping on given OfferCardView) which entry from map is being clicked on/view filled with. I don’t know how to acomplish this, I’m aware that using Map here is bad decision, but I don’t have clue how this should be done

2

Answers


  1. You can share variables from other controllers onto another controller by using GetX Dependency Injection

    On binding , add the controller you want to add as a dependency

    Get.lazyPut<OfferCardsController>(() => OfferCardsController());
    

    then in the controller

    var offerCardsController = Get.find<OfferCardsController>();
    

    you can now access variables from the OfferCardsController onOfferController

    e.g

    offerCardsController.variableFromCardsController;
    
    Login or Signup to reply.
  2. The better practice is passing each document data with its index to the OfferView() constructor, so for every OfferCardView() that will be clicked, OfferView() will be opened with that data.

    This ensures that your data will not rely on the GetxController availability, since depending on GetxController to exchange data like this could simply break.

    For example :

    While your app is growing and somewhere the controller is deleted either by Getx or manually using Get.delete() ( or you needed to call multiple controllers with different tags ), then Get.find() will not find that controller or mistake it, this leads to unexpected behaviors, which will put you in a hard time to find out what went wrong in your project.

    Using GetPage, if you’re required to assign the model data property, you could make a placeholder model for that data by default where we would say like :

    There is no data so we showed you that placeholder alternative data page with this data.

    This gives the user at least an overview of what’s happening, not just a direct crash for the app.

    I would say it’s a good practice for the user experience.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search