setState() or markNeedsBuild() called during build problem

22 Views Asked by At

I'm trying to remove the consumer

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
      final provider = Provider.of<AppDataProvider>(context, listen: false);
      provider.getChapterByID(widget.courseid);
      provider.getLessonById(widget.courseid);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Consumer<AppDataProvider>(
          builder: (context, provider, _) {
            List<Chapter> chapters = provider.allchapter;
            List<Lesson> lessons = provider.lesson;
            if (chapters.isEmpty) {
              return const Center(
                child: Text('No chapters available'),
              );
            } else {
              return ListView.builder(
                itemCount: chapters.length,
                itemBuilder: (context, index) {
                  Chapter c = chapters[index];
                  List<Lesson> chapterLessons = lessons
                      .where((lesson) => lesson.chpaterID == c.id)
                      .toList();
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          c.title,
                          style: const TextStyle(
                            fontSize: 30,
                            fontWeight: FontWeight.bold,
                            color: tdBlue,
                          ),
                        ),
                      ),
                      if (chapterLessons.isNotEmpty)
                        Column(

I'm trying to remove the Consumer but then no data shown from provider its in many page when i use consumer i got this error, idonot know if its normal error :

enter image description here

0

There are 0 best solutions below