Search code examples
flutterwidgetsetstatestateless

SOLVED! setState() or markNeedsBuild() called during build ,stuck on this although the app work normally


This is my home screen and it is working fine but it is throwing error each time I navigate to it. I don't know what to do , all of the app is working normally , it is just throwing this errors and I want to fix this.

Solution found : added the code block given by Jonathan Ixcayau to my MoviesProvider :

@override
  void notifyListeners() {
    WidgetsBinding.instance.addPostFrameCallback(
      (_) {
        super.notifyListeners();
      },
    );
  }

The class:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:gap/gap.dart';
import 'package:ronasapp/providers/providers.dart';
import 'package:ronasapp/screens/screens.dart';
import 'package:ronasapp/utils/extensions.dart';
import '../widgets/widgets.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});
  

  @override
  Widget build(BuildContext context) {
    final movieProvider = Provider.of<Movies>(context, listen: false);
    final directorProvider = Provider.of<Directors>(context, listen: false);
    final genreProvider = Provider.of<Genres>(context, listen: false);

    return Scaffold(
      extendBody: true,
      resizeToAvoidBottomInset: true,
      body: SafeArea(
        child: Stack(
          children: [
            SingleChildScrollView(
              child: Padding(
                padding:
                    const EdgeInsets.symmetric(vertical: 16.0, horizontal: 16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 10),
                      child: Text(
                        "Popular directors",
                        style: context.textTheme.titleLarge!.copyWith(
                          fontWeight: FontWeight.w400,
                          fontSize: 26,
                        ),
                      ),
                    ),
                    //directors list
                    SizedBox(
                      height: 140,
                      child: ListView.separated(
                        scrollDirection: Axis.horizontal,
                        itemCount: directorProvider.directors.length,
                        itemBuilder: (context, index) {
                          final director = directorProvider.directors[index];
                          return GestureDetector(
                            onTap: () {
                              Navigator.of(context).pushNamed(
                                  DirectorDetails.routeName,
                                  arguments: director);
                            },
                            child: DirectorAvatar(
                              director: director,
                            ),
                          );
                        },
                        separatorBuilder: ((context, index) => const Gap(5)),
                      ),
                    ),
                    const WhiteLargeText(text: "Trending"),
                    const Gap(10),
                    //new movies
                    FutureBuilder(
                      future: movieProvider.getTrendingMovies(),
                      builder: (context, snapshot) {
                        if (snapshot.connectionState ==
                            ConnectionState.waiting) {
                          return const Center(
                            child: CircularProgressIndicator(),
                          );
                        } else if (snapshot.hasData) {
                          final data = snapshot.data;
                          return MovieCardsList(movies: data!);
                        } else if (snapshot.hasError) {
                          return Text(snapshot.error.toString());
                        } else {
                          return const Text("Error");
                        }
                      },
                    ),
                    const WhiteLargeText(text: "Genres"),
                    const Gap(10),
                    FutureBuilder(
                        future: genreProvider.getGenres(),
                        builder: (context, snapshot) {
                          if (snapshot.connectionState ==
                              ConnectionState.waiting) {
                            return const Center(
                                child: CircularProgressIndicator());
                          } else if (snapshot.hasData) {
                            return SizedBox(
                              height: 110,
                              child: ListView.separated(
                                scrollDirection: Axis.horizontal,
                                itemBuilder: (context, index) {
                                  final genre = genreProvider.genres[index];
                                  final moviesInGenre =
                                      movieProvider.getMoviesByGenre(genre.id);
                                  return GestureDetector(
                                      onTap: () {
                                        Navigator.of(context).pushNamed(
                                          SpecificCategoryScreen.routeName,
                                          arguments: [
                                            genre.name,
                                            moviesInGenre
                                          ],
                                        );
                                      },
                                      child: GenreCard(genre: genre));
                                },
                                separatorBuilder: (context, index) =>
                                    const Gap(10),
                                itemCount: genreProvider.genres.length,
                              ),
                            );
                          } else {
                            return const Text("Error");
                          }
                        }),

                    const Gap(20),
                    const WhiteLargeText(text: "Coming soon"),
                    const Gap(10),
                    const ComingSoonMovieCard(),
                    // const Gap(70),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

The error is this : setState() or markNeedsBuild() called during build. I used MediaQuery on another screen , and in the error I saw that it say ,widget builld when calling MediaQuery , but I couldn't find a link to it.


Solution

  • Check which provider is provoking the error, and add this code to that provider

      @override
      void notifyListeners() {
        WidgetsBinding.instance.addPostFrameCallback(
          (_) {
            super.notifyListeners();
          },
        );
      }