flutterbloc

Attempting to hot reload Flutter app restarts whole app


I'm working on a Flutter app and I've noticed that whenever I save my app to see the updated changes, my entire app reloads. I would expect the page that I have open to hot reload with my new changes on it.

I think that the way my widget tree is organised could have an effect on this. My entire app is wrapped in a widget that runs at startup that initialises to a ServicesLoadingState. Then, after the services have finished starting up (in the BLoC), it yields a ServicesReadyState and then all my widgets are drawn. I think when I hot reload, the state of the topmost widget switches back to ServicesLoadingState, reloads the services, and shows me the behaviour I'm experiencing.

I've tried googling "flutter hot reload restarts app" but I haven't found any good tips on how to prevent this. How can I stop hot reload from restarting the entire app?


Solution

  • If you use navigatorKey, you get this issue when you set a non static value to the property navigatorKey of MaterialApp.

    A proper usage of navigatorKey can be to create a class and define a static property for navigatorKey like that :

    class NavigationService {
      static GlobalKey<NavigatorState> navigatorKey =
          new GlobalKey<NavigatorState>();
    }
    

    then in the MaterialApp, you set it like that

    MaterialApp(
      navigatorKey: NavigationService.navigatorKey,
      home: MyApp(),
    );