Home > OS >  Extract and match index in two loops in Dart
Extract and match index in two loops in Dart

Time:09-13

Could you help me to understand how to filter a Future List (snapshot data) based on my current index? If I had itembuilder with index param would be easier, but the widget I am using does not have this like ListView.builder. Where I am struggling is where I am pointing out with the arrows. I cannot match the information based on the result of the first loop. Is there any way to extract the current index of the first loop so that I can match the information in the second one?

                  widgets: [
                     -----> for (var dog in snapshot.data!)
                      Card(
                        elevation: 10,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(15.0),
                        ),
                        child: Column(
                          children: [
                            Expanded(
                              child: Container(
                                decoration: const BoxDecoration(
                                  borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(15),
                                      topRight: Radius.circular(15)),
                                  image: DecorationImage(
                                    image: NetworkImage(
                                        AppConstants.QUESTION_MARK_IMG
                                        //AppConstants.BACK4APP_IMG_URL   dog['DogImg']
                                        ),
                                    fit: BoxFit.cover,
                                  ),
                                ),
                              ),
                            ),
                            Expanded(
                              child: Container(
                                padding: const EdgeInsets.all(10),
                                child: Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  children: [
                                    Text(
                                      dog['title'],
                                      style: const TextStyle(
                                          fontSize: 20,
                                          fontWeight: FontWeight.bold),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                  ],
                ),
              ),
              -----> for (var dogs in snapshot.data!.where((element) =>
                  element['_id'] ==
                  snapshot.data![currentIndex!]['_id']))
                Expanded(
                    child: SlidingUpPanel(
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(24.0),
                            topRight: Radius.circular(24.0)),
                        maxHeight:
                            MediaQuery.of(context).size.height / 3,
                        defaultPanelState: PanelState.OPEN,
                        panel: Center(
                            child: Text(dogs['DogDescription']))))
            ],
          ),
        ],
      );

CodePudding user response:

Instead of this:

for(var dog in snapshot.data!)

Try this:

for(int index = 0; i < (snapshot.data! as List).length; i  )

It will give you the index

  • Related