skip to Main Content

I have two lists: the first list is called PlanExerciseslist. Its data are retrieved from Firestore and the second list is list of checkboxes… Here is the code to understand:

late List<bool> isChecked;
isChecked = List<bool>.filled(LifeStyleCubit.get(context).PlanExerciseslist.length, false);

The problem is that List<bool> isChecked doesn’t have any list yet of PlanExerciseslist.length, because PlanExerciseslist takes some time to bring the data from Firestore. So it gives me a Range error (index).

Here is my full code:

class BackPlanScreen extends StatefulWidget {
  PlanQsModel model;
  int dayNum;
  int weekNum;
  BackPlanScreen(this.model, this.dayNum, this.weekNum);

  @override
  State<BackPlanScreen> createState() => _BackPlanScreenState();
}

class _BackPlanScreenState extends State<BackPlanScreen> {
  late List<bool> isChecked;
  @override
  void initState() {
    super.initState();
    isChecked = List<bool>.filled(LifeStyleCubit.get(context).PlanExerciseslist.length, false);
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<LifeStyleCubit, LifeStates>(
      listener: (context, state)
      {},
      builder: (context, state)
      {
        var cubit = LifeStyleCubit.get(context);
        cubit.getPlanBackEx1(widget.model.level);
        return Scaffold(
          appBar: AppBar(
            title: Text('Exercise'),
          ),
          body: Padding(
            padding: const EdgeInsets.all(20.0),
            child: ListView.separated(
                scrollDirection: Axis.vertical,
                itemBuilder:(context, index) => BackWidget(cubit.PlanExerciseslist[index], context, index),
                separatorBuilder: (context, index) => SizedBox(height: 35,),
                itemCount: cubit.PlanExerciseslist.length
            ),
          ),
        );
      },
    );
  }

  Widget BackWidget (PlanExercisesModel model, context, index) => GestureDetector(
    onTap: () => Navigator.push(context,
        MaterialPageRoute(builder: (context) => BackPlanGifScreen(model))),
    child: Builder(
      builder: (context) {
        return Container(
          height: 100,
          width: MediaQuery.of(context).size.width,
          child: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: [
                Checkbox(
                    value: isChecked[index],
                    activeColor: Colors.orangeAccent,
                    onChanged: (newBool) {
                      setState(() {
                        isChecked[index] = newBool!;
                        if (isChecked[index] == true)
                        {
                          LifeStyleCubit.get(context).createPlanExercisesDay(
                            name: model.name,
                            difficulty: model.difficulty,
                            bodyPart: model.bodyPart,
                            equipment: model.equipment,
                            gifURL: model.gifURL,
                            target: model.target,
                            weekNum: widget.weekNum + 1,
                            dayNum: widget.dayNum + 1,
                            id: widget.model.description,
                          );
                        }
                        if(isChecked[index] == false) {
                          LifeStyleCubit.get(context).deletePlanExercisesDay(
                              weekNum: widget.weekNum + 1,
                              dayNum: widget.dayNum + 1,
                              id: widget.model.description,
                              name: model.name
                          );
                        }
                      });
                    }
                ),
              ],
            ),
          ),
        );
      }
    ),
  );
}

How can I solve this problem? Or in another way, how can I make the List checkBox wait for like 3 seconds until the other list is fully retrieved from Firebase?

2

Answers


  1. You have to use the async await function.
    Fetching data from Firestore takes time, and it will wait until it gets the data.

    If you want to wait for a fixed time, use timer.

    Login or Signup to reply.
  2. You should prefer using await to wait to get data from Firebase.

    Once you have got it, try using setState to set the variable isChecked.

    Else consider using FutureBuilder if the function is defined as future.

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