Home > Mobile >  flutter Force listview builder to show only 1 item based on condition
flutter Force listview builder to show only 1 item based on condition

Time:12-16

I am making a to-do project using hive and I want to show the todos that are marked as completed and not completed in a different row. Now when the mark as completed row is empty I want to show a message like "You can mark todos as completed after completing the todo". I have implemented these features but the problem is the message "You can mark todos as completed after completing the todo" repeats with the number of items in the todo. for example if the todo contains 2 todos and both are not completed then I get the message:

"You can mark todos as completed after completing the todo" "You can mark todos as completed after completing the todo"

Code I am working with:

ListView.builder(
                  primary: false,
                  itemCount: Hive.box___.length,
                  itemBuilder: (context, index) {
                    Todo? data = Hive.box____.getAt(index);
                    return data!.isCompleted
                        ? _______ //show the todos that are completed
                        : const Center(
                            child: Text(
                              "You can mark todos as completed after completing the todo"
                            ),
                          );
                  },

CodePudding user response:

you have to also put a condition on itemCount.

so you have 2 listviews. one for not completed and one for completed items.

in each ListView, you have to filter your list so filtered items are passed to each list. in the completed list view you write

itemCount: completedItems.lenth == 0 ? 1 : completedItems.lenth

and in builder, you have to check if completedItems.lenth is zero then return Text else return the corresponding card.

CodePudding user response:

I have solved the issue, first I have filtered and checked if there are no completed items if it is so, I have shown item count as 1 else it will be the length of the to-do list.

here is my code

ListView.builder(
                  primary: false,
                  itemCount: Hive.box___.length,
                  itemBuilder: (context, index) {
                    Todo? data = Hive.box__.values
                          .where((element) => element.iscompleted == true)
                          .isEmpty
                      ? 1
                      : Hive.box____.length,;
                    return data!.isCompleted
                        ? _______ //show the todos that are completed
                        : const Center(
                            child: Text(
                              "You can mark todos as completed after completing the todo"
                            ),
                          );
                  },

Also my code is inspired from the one shared by Riza

CodePudding user response:

implement data outside builder

 List<Todo>? data = Hive.box____.get('name')
 int count = Hive.box___.length;
        ListView.builder(
                          primary: false,
                          itemCount:count,
                          itemBuilder: (context, index) {
                           );
                            if(data![index].isCompleted){
                                return _______ //show the todos that are completed
}
                                else {return const Center(
                                   child: Text(
                                      "You can mark todos as completed after completing the todo"
                                    ),
                                  );
count = 0;
}
                          },
  • Related