Flutter: Update Widgets On Resume?

后端 未结 6 1539
梦如初夏
梦如初夏 2020-12-01 03:06

In Flutter, is there a way to update widgets when the user leaves the app and come right back to it? My app is time based, and it would be helpful to update the time as soon

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 03:34

    For deeply testing, I think the results are worth for read. If you are curious about which method you should use, just read the below: (Tested on Android)

    There are three methods for LifeCycle solution.

    1. WidgetsBindingObserver
    2. SystemChannels.lifecycle
    3. flutter-android-lifecycle-plugin

    The main difference between WidgetsBindingObserver and SystemChannels.lifecycle is that WidgetsBindingObserver have more capables If you have a bunch of widgets that need to listen LifeCycle. SystemChannels is more low layer, and used by WidgetsBindingObserver.

    After several testing, If you use SystemChannels after runApp, and home widget mixin with WidgetsBindingObserver, home widget would be failed, because SystemChannels.lifecycle.setMessageHandler override the home's method.

    So If you want to use a global, single method, go for SystemChannels.lifecycle, others for WidgetsBindingObserver.

    And what about the third method? This is only for Android, and If you must bind your method before runApp, this is the only way to go.

提交回复
热议问题