Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter initState() returns a _debugLifecycleState error

i have been building flutter app for a while now and everything seems to work fine but today i tried calling initState() in a statefulWidget so i can make an API call but it returns an error that i am unfamiliar with

My Code:

class Home extends StatefulWidget {

  final String header;

  Home({Key key, this.header}): super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home>{
  @override
  Widget build(BuildContext context) {

    Top top;
    bool _loading = true;

    fetchData() async {
      setState(() {
        _loading = true;
      });
      //Fetch top Animes
      var res = await http.get(Constants.topAnimes);
      var decodedJson = jsonDecode(res.body);
      print(decodedJson['top']);
      top = Top.fromJson(decodedJson['top']);
      print(top.toJson());


      setState(() {
        _loading = false;
      });
    }

    @override
    void initState() {
      super.initState();
      fetchData();
    }

    return Container();
  }
}

The Error:

I/flutter (24395): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/View    (24395): Key down dispatch to io.flutter.view.FlutterView{8dd5ba8 VFE...... .F...... 0,0-720,1200}, event = KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_F11, scanCode=87, metaState=0, flags=0x8, repeatCount=11805, eventTime=9200503, downTime=8597691, deviceId=2, source=0x101 }
I/flutter (24395): The following assertion was thrown building Home(dirty, state: _HomeState#5567f):
I/flutter (24395): 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1008 pos 12:
I/flutter (24395): '_debugLifecycleState == _StateLifecycle.created': is not true.
I/View    (24395): Key down dispatch to io.flutter.view.FlutterView{8dd5ba8 VFE...... .F...... 0,0-720,1200}, event = KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_F11, scanCode=87, metaState=0, flags=0x8, repeatCount=11806, eventTime=9200553, downTime=8597691, deviceId=2, source=0x101 }
I/flutter (24395): 
I/flutter (24395): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (24395): more information in this error message to help you determine and fix the underlying cause.
I/flutter (24395): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (24395):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (24395): 
I/flutter (24395): When the exception was thrown, this was the stack:
I/flutter (24395): #2      State.initState (package:flutter/src/widgets/framework.dart:1008:12)
I/flutter (24395): #3      _HomeState.build.initState (package:flutter_anime/screens/home.dart:66:13)
I/flutter (24395): #4      _HomeState.build (package:flutter_anime/screens/home.dart:70:14)
I/flutter (24395): #5      StatefulElement.build (package:flutter/src/widgets/framework.dart:3825:27)
I/flutter (24395): #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3736:15)
I/flutter (24395): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #11     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #12     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #13     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #14     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #16     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #17     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #18     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #19     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #20     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #22     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #28     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #29     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #30     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #31     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #32     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #35     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #36     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #37     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #38     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #39     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #40     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #41     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #42     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #43     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #44     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #45     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #46     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #47     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #48     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #49     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #50     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #51     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #52     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #53     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #54     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #55     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #56     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #57     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #58     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #59     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #60     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #61     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #62     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #63     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/View    (24395): Key down dispatch to io.flutter.view.FlutterView{8dd5ba8 VFE...... .F...... 0,0-720,1200}, event = KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_F11, scanCode=87, metaState=0, flags=0x8, repeatCount=11809, eventTime=9200704, downTime=8597691, deviceId=2, source=0x101 }
I/flutter (24395): #64     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #65     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #66     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #67     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #68     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #69     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #70     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #71     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #72     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #73     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #74     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #75     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #76     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #77     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #78     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #79     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #80     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #81     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #82     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #83     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #84     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #85     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #86     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #87     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #88     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #89     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #90     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #91     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #92     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #93     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #94     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #95     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #96     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #97     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #98     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #99     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #100    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #101    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4982:32)
I/flutter (24395): #102    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #103    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #104    _TheatreElement.mount (package:flutter/src/widgets/overlay.dart:494:16)
I/flutter (24395): #105    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #106    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #107    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #108    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #109    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #110    StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #111    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #112    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #113    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #114    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #115    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #116    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #117    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #118    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #119    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #120    SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #121    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #122    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #123    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #124    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #125    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #126    StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #127    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #128    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #129    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #130    SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #131    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #132    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #133    SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #134    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #135    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #136    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #137    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #138    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #139    StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11) 
I/flutter (24395): #140    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #141    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #142    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #143    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #144    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #145    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #146    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #147    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #148    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #149    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #150    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #151    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #152    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #153    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #154    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #155    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #156    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #157    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #158    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #159    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #160    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #161    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #162    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #163    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #164    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #165    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #166    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #167    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #168    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #169    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #170    StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #171    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #172    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #173    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #174    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #175    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #176    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #177    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #178    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #179    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #180    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #181    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #182    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #183    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #184    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #185    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #186    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #187    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #188    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #189    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #190    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #191    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #192    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #193    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #194    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #195    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #196    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #197    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #198    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #199    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #200    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #201    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #202    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #203    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #204    SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
I/flutter (24395): #205    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #206    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #207    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #208    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #209    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #210    StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #211    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #212    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #213    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #214    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #215    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #216    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #217    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #218    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #219    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #220    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #221    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #222    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #223    StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #224    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #225    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #226    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #227    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #228    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #229    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #230    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #231    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #232    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #233    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #234    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #235    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #236    StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
I/flutter (24395): #237    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #238    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #239    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #240    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (24395): #241    Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (24395): #242    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
I/flutter (24395): #243    ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
I/flutter (24395): #244    Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
I/flutter (24395): #245    Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
I/flutter (24395): #246    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:933:16)
I/flutter (24395): #247    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:904:5)
I/flutter (24395): #248    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:850:17)
I/flutter (24395): #249    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2253:19)
I/flutter (24395): #250    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:849:13)
I/flutter (24395): #251    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:736:7)
like image 491
JideGuru Avatar asked Apr 16 '19 06:04

JideGuru


3 Answers

All of your state properties and initState method are in your build method. Move them outside of build method. Also, initial value of the variable _loading is true so there is no need to set it to true again at the beginning of the fetchData function.

class Home extends StatefulWidget {
  final String header;

  Home({Key key, this.header}): super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home>{
  Top top;
  bool _loading = true;

  @override
  void initState() {
    super.initState();
    fetchData();
  }

  @override
  Widget build(BuildContext context) {
    if(_loading) {
        return CircularProgressIndicator();
    }

    return Container(
        child: Text("LOADED");
    );
  }

  fetchData() async {
    //Not needed since it's already set to true.
    /*setState(() {
      _loading = true;
    });*/

    //Fetch top Animes
    var res = await http.get(Constants.topAnimes);

    var decodedJson = jsonDecode(res.body);
    print(decodedJson['top']);

    top = Top.fromJson(decodedJson['top']);
    print(top.toJson());

    setState(() {
      _loading = false;
    });
  }
}
like image 116
Anis Alibegić Avatar answered Oct 18 '22 01:10

Anis Alibegić


There is no reason for calling setState() within a initState() method. Set state is to trigger a rebuild when you change a variable. For example if you like to use the _loading boolean to display some sort of loading indicator widget you want to call setState() in order to let Flutter rebuild the tree.

But the important part is that you only need setState() when you change a variable during the lifecycle of the widget. Meaning after initState() is finished and you widget is build. You usually call setState() when you f.ex. press a button to trigger the API call. Then you would like to set _loading within a setState() to rebuild the widget.

In the initState() method however no tree is built. Every variable you change during this function call will be reflected once the widget builds. Simply get rid of the setState() and directly set your variables without it.

I guess this cleared up this question. If not.. what are you using _loading for anyways?

like image 37
Robin Reiter Avatar answered Oct 18 '22 02:10

Robin Reiter


I have solved the error... just write dispose function.

@override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    // animationController.dispose() instead of your controller.dispose 
  }
like image 30
Нараа Болорхүү Avatar answered Oct 18 '22 01:10

Нараа Болорхүү