Attempting to hot reload Flutter app restarts whole app
Asked Answered
S

1

18

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?

Sudanic answered 15/8, 2019 at 21:36 Comment(2)
Likely related: #52250078Reverend
I am facing a similar issue. In my case, I am using Provider inside my widget.Caracara
R
14

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(),
);
Reprisal answered 1/8, 2021 at 19:31 Comment(3)
Works to me, i commented the navigatorKey on my main. Thank youParcae
This was not the answer for me. Any other solution? I am using riverpod state management.Draco
I solved the problem by using StatefulWidget instead of StatelessWidgetPt

© 2022 - 2024 — McMap. All rights reserved.