Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You forgot () in end

try this

editFunction: () => _showEditDialog(),

or this

editFunction: _showEditDialog,
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading