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

前端 未结 7 1461
半阙折子戏
半阙折子戏 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.

    0 讨论(0)
  • 2021-02-18 17:20

    Use the didChangeDependencies method which gets called after initState.

    For your example:

    @override
    void initState() {
      super.initState();
    }
    
    @override
    void didChangeDependencies() {
      super.didChangeDependencies();
    
      this._getCategories();
    }
    
    void _getCategories() async {
      // Omitted for brevity
      // ...
    
     }
    
    0 讨论(0)
  • 2021-02-18 17:20

    The best solution i think is use the context from the Widget build. And paste the method _getCategories(context) after the build with the context from the tree. So there is no problem with the widget tree.

    0 讨论(0)
  • 2021-02-18 17:23

    Adding a frame callback might be better than using Future.delayed with a zero duration - it's more explicit and clear as to what is happening, and this kind of situation is what frame callback was designed for:

    @override
      void initState() {
        super.initState();
    
        WidgetsBinding.instance.addPostFrameCallback((_) async {
          _getCategories();
        });
      }
    
    0 讨论(0)
  • 2021-02-18 17:23

    There are many ways to solve this problem, override initState method:

    @override
    void initState() {
      super.initState();
      // Use any of the below code here. 
    }
    
    • Using SchedulerBinding mixin:

      SchedulerBinding.instance.addPostFrameCallback((_) {
        // Call your function
      });
      
    • Using Future class:

      Future(() {
        // Call your function
      });
      
    • Using Timer class:

      Timer(() {
        // Call your function
      });
      
    0 讨论(0)
  • 2021-02-18 17:34

    an alternative is to put it inside PostFrameCallback which is between initState and Build so we can be sure that the initState is already finished.

     @override
      void initState() {
        WidgetsBinding.instance.addPostFrameCallback((_) => getData());
        super.initState();
      }
    
      getData() async {
    
      }
    
    0 讨论(0)
提交回复
热议问题