Search code examples
fluttermobileruntime-errorparameter-passing

Flutter: setState() or markNeedsBuild called during build


I am receiving an error when passing .There is a question here from 5 years ago with the same title. I do not know if I am implementing the solution incorrectly or it has been changed. My code is below. I know the problem lays in the _showEditDialog function and it's passing to my exerciseTable. AlertDialog calls build for some reason. I just do not know how to fix it.

    showDialog(
      context: context,
      builder: (context) => AlertDialog(title: Text(exercise.name)),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ExerciseTable(
      userExercises: todaysExercises,
      editFunction: () => _showEditDialog,
    );
  } 
  final List<Exercise> userExercises;
  Function editFunction;

  ExerciseTable(
      {required this.userExercises, super.key, required this.editFunction});

  TextStyle headerTextStyle =
      const TextStyle(fontWeight: FontWeight.bold, fontSize: 30);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(15),
      child: Column(children: [
        SizedBox(
          height: 500,
          child: ListView.builder(
            itemCount: userExercises.length,
            itemBuilder: (context, index) {
              return ListTile(
                leading: CircleAvatar(
                  child: userExercises[index].changeWeight,
                ),
                title: Text(userExercises[index].name),
                subtitle: Text("${userExercises[index].reps} Reps"),
                trailing: Text("${userExercises[index].sets} Sets"),
                onTap: () => editFunction(userExercises[index]),
              );
            },
          ),
        )
      ]),
    );
  } 

The other post suggested wrapping my _showEditDialog with

WidgetsBinding.instance.addPostFrameCallback((_){ // Add code here}

and I tried but did not understand how to implement it. Thank You!


Solution

  • You forgot () in end

    try this

    editFunction: () => _showEditDialog(),
    

    or this

    editFunction: _showEditDialog,