手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2022年06月28日的文章

flutter中页面渲染完成的回调

 这玩意就有点象vue中的mounted/或者其他的afterLoad等操作。怎么突然想要用到这个呢,是因为我用了getX,之前用riverpod,也没注意过这个问题。用了GetX之后,这个东西就被派上用场了。

原因是我有一个设置背景图的功能。本来想着一切换页面,背景图就加载出来,但事实上我把这个功能提前后,进入页面就直接报错,说是obx在页面没有渲染完成的时候不能调用。

This Obx 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.

于是,使用:

WidgetsBinding.instance.addPostFrameCallback((time) { 

     final back = Get.find<BackgroundController>(); 

     back.set(Assets.backgroundImage); 

});

问题解决