Unhandled Exception: inheritFromWidgetOfExactType(_LocalizationsScope) or inheritFromElement() was called before _ScreenState.initState() completed

前端 未结 7 1463
半阙折子戏
半阙折子戏 2021-02-18 16:46

I am calling initial method to load data from API using initState. But it is resulting me an error. Here is error:

Unhandled Exception: inheritFromWidgetOfExactT         


        
7条回答
  •  暖寄归人
    2021-02-18 17:17

    You need to call _getCategories after initState has completed.

    
    @override
    void initState() {
       super.initState();
    
       Future.delayed(Duration.zero, () {
          this._getCategories();
       });
    
       // Could do this in one line: Future.delayed(Duration.zero, this._getCategories);
    }
    
    

    Also, you could do this on a different way, using addPostFrameCallback like showed in another answers.

提交回复
热议问题