Home > Net >  Flutter provider.of unexpected problem with PageView.builder
Flutter provider.of unexpected problem with PageView.builder

Time:11-29

I am writing a program with a simple page scroller that changes pages when scrolled. I want to have these pages changed this way such that I can be able to change them with another widget that will be inserted into the stack. Currently, I am not fully understanding the error and would like to know why the error is thrown. The error is shown below. Each screen page item has only a Scaffold with text inside body.

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../screens/account.dart';
import '../screens/home.dart';
import '../screens/post.dart';
import '../screens/saved.dart';


class ScreenChanger extends StatefulWidget {
  const ScreenChanger({Key? key}) : super(key: key);

  @override
  State<ScreenChanger> createState() => _ScreenChangerState();
}

class _ScreenChangerState extends State<ScreenChanger> {
  late HomeScreen homeScreen;
  late SavedScreen savedScreen;
  late PostScreen postScreen;
  late AccountScreen accountScreen;

  @override
  void initState() {

    homeScreen=HomeScreen();
    savedScreen=SavedScreen();
    postScreen=PostScreen();
    accountScreen=AccountScreen();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ChangeNotifierProvider(
        create: (_) { return ScreenController(); },
        builder: (context, child) {
          return Stack(
            children: [
              PageView.builder(
                itemCount: 4,
                itemBuilder: (context, index) {
                  setState(() {
                    Provider.of<ScreenController>(context, listen: false).currentPage=index;
                  });
                  return [
                    homeScreen,
                    savedScreen,
                    postScreen,
                    accountScreen,
                  ][Provider.of<ScreenController>(context).currentPage];
                },
              ),
            ],
          );
        },
      ),
    );
  }
}

class ScreenController extends ChangeNotifier{
  late int _currentPage;
  int get currentPage => _currentPage;

  set currentPage(int value) {
    _currentPage = value;
    notifyListeners();
  }

  ScreenController({
    int currentPage = 0
  }){
    this.currentPage=currentPage;
  }

}

Error

======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for ScreenController:
setState() or markNeedsBuild() called during build.

This _InheritedProviderScope<ScreenController?> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _InheritedProviderScope<ScreenController?>
  value: Instance of 'ScreenController'
  listening to value
The widget which was currently being built when the offending call was made was: SliverFillViewport
When the exception was thrown, this was the stack: 
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4549:11)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4564:6)
#2      _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:577:5)
#3      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:351:24)
#4      ScreenController.currentPage= (package:real_estate_v02/widgets/screen_changer.dart:69:5)
#5      _ScreenChangerState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:real_estate_v02/widgets/screen_changer.dart:45:75)
#6      State.setState (package:flutter/src/widgets/framework.dart:1114:30)
#7      _ScreenChangerState.build.<anonymous closure>.<anonymous closure> (package:real_estate_v02/widgets/screen_changer.dart:44:19)
#8      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:485:22)
#9      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1376:28)
#10     SliverMultiBoxAdaptorElement.performRebuild.processElement (package:flutter/src/widgets/sliver.dart:1306:67)
#11     Iterable.forEach (dart:core/iterable.dart:325:35)
#12     SliverMultiBoxAdaptorElement.performRebuild (package:flutter/src/widgets/sliver.dart:1353:24)
#13     SliverMultiBoxAdaptorElement.update (package:flutter/src/widgets/sliver.dart:1282:7)
#14     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#15     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#16     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#17     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#18     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#19     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#20     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#21     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#22     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#23     _ViewportElement.update (package:flutter/src/widgets/viewport.dart:237:11)
#24     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#25     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#26     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#27     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#28     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#29     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#30     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#31     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#32     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#33     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#34     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#35     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#36     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#37     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#38     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#39     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#40     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#41     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#42     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#43     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#44     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#45     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#46     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#47     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#48     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#49     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#50     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#51     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#52     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#53     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#54     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#55     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#56     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#57     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#58     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#59     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#60     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#61     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#62     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#63     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#64     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#65     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#66     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#67     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#68     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#69     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#70     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#71     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#72     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#73     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#74     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#75     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#76     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#77     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#78     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#79     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#80     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#81     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#82     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#83     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#84     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#85     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#86     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#87     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#88     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#89     _InheritedProviderScopeElement.update (package:provider/src/inherited_provider.dart:523:11)
#90     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#91     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#92     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#93     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#94     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#95     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#96     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#97     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#98     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#99     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#100    Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
.......
#350    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
#351    SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:881:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
The ScreenController sending notification was: Instance of 'ScreenController'
====================================================================================================

======== Exception caught by widgets library =======================================================
The following assertion was thrown building:
setState() or markNeedsBuild() called during build.

This ScreenChanger widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: ScreenChanger
  state: _ScreenChangerState#ffffa
The widget which was currently being built when the offending call was made was: SliverFillViewport
When the exception was thrown, this was the stack: 
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4549:11)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4564:6)
#2      State.setState (package:flutter/src/widgets/framework.dart:1134:15)
#3      _ScreenChangerState.build.<anonymous closure>.<anonymous closure> (package:real_estate_v02/widgets/screen_changer.dart:44:19)
#4      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:485:22)
#5      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1376:28)
#6      SliverMultiBoxAdaptorElement.performRebuild.processElement (package:flutter/src/widgets/sliver.dart:1306:67)
#7      Iterable.forEach (dart:core/iterable.dart:325:35)
#8      SliverMultiBoxAdaptorElement.performRebuild (package:flutter/src/widgets/sliver.dart:1353:24)
#9      SliverMultiBoxAdaptorElement.update (package:flutter/src/widgets/sliver.dart:1282:7)
#10     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#11     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#12     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#13     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#14     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#15     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#16     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#17     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#18     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#19     _ViewportElement.update (package:flutter/src/widgets/viewport.dart:237:11)
#20     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#21     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#22     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#23     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#24     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#25     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#26     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#27     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#28     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#29     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#30     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#31     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#32     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#33     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#34     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#35     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#36     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#37     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#38     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#39     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#40     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#41     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#42     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#43     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#44     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#45     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#46     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#47     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#48     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#49     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#50     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#51     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#52     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#53     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#54     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#55     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#56     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#57     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#58     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#59     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#60     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#61     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#62     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#63     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#64     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#65     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#66     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#67     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#68     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#69     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#70     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#71     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#72     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#73     StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#74     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#75     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#76     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#77     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#78     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#79     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#80     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#81     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#82     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#83     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#84     ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#85     _InheritedProviderScopeElement.update (package:provider/src/inherited_provider.dart:523:11)
#86     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#87     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#88     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#89     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#90     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#91     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#92     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#93     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#94     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#95     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#96     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#97     StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#98     Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#99     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#100    Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
.....
#345    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1175:15)
#346    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
#347    SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:881:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
====================================================================================================

CodePudding user response:

You are calling setState middle of widget build. You can wait to finish the frame and then call setState.

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      Provider.of<ScreenController>(context, listen: false).currentPage = index;
    }); // you may not needed using setState because there is not state variable needed to be updated
  • Related