Flutter: Call a function on a child widget's state

后端 未结 4 1734
小蘑菇
小蘑菇 2021-02-04 06:40

I\'ve created a stateful widget and its parent widget needs to call a function that lives in the child\'s state.

Specifically, I have a class PlayerContainer that create

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-04 06:57

    While Darren Cole's answer above does not work correctly, there is an easy way to circumvent the immediate problem. Instead of

    State createState() => vpcs;
    
    final VideoPlayerControllerState vpcs = VideoPlayerControllerState();
    

    write:

    State createState(){
      vpcs = VideoPlayerControllerState();
      return vpcs;
    }
    
    VideoPlayerControllerState vpcs;
    
    

    This way, the state gets re-written everytime createState() is called, which avoids the Failed assertion: line 3819 pos 12: '_state._widget == null': is not true errors.

    Still, I guess this is a somewhat hackish solution - can somebody point out a better one?

提交回复
热议问题