getx icon indicating copy to clipboard operation
getx copied to clipboard

[Flutter web] Hot Reload removes all routes except the current

Open dakr0013 opened this issue 4 years ago • 21 comments
trafficstars

Describe the bug If i use Get.toNamed() to navigate to other pages and do a hot reload all other pages get lost except the current page. Therefore if i try to navigate back (after a hot reload) i get to an empty page. Also if i use a controller of a previous page i get a null error after hot reloading. But if i use Navigator.pushNamed() and do a hot reload, my pages are still there and i can navigate back.

Reproduction code NOTE: THIS IS MANDATORY, IF YOUR ISSUE DOES NOT CONTAIN IT, IT WILL BE CLOSED PRELIMINARY)

example:

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() {
  runApp(GetMyApp());
  // runApp(MyApp());
  print("app started");
}

// ----------------------------------------------------------------------------
// navigation with flutters Navigator.pushNamed(...)
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      routes: {
        "/": (_) => LoginPage(),
        "/register": (_) => RegisterPage(),
      },
      initialRoute: "/",
      navigatorObservers: [
        MyNavigatorObserver(),
      ],
    );
  }
}

class MyNavigatorObserver extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
    print("navigated to: ${route.settings.name}");
  }
}

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("login"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("register"),
          onPressed: () => Navigator.pushNamed(context, "/register"),
        ),
      ),
    );
  }
}

class RegisterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("register"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("back"),
          onPressed: () => Navigator.pop(context),
        ),
      ),
    );
  }
}

// ----------------------------------------------------------------------------
// navigation with gets Get.toNamed(...)
class GetMyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Get Demo',
      getPages: [
        GetPage(
          name: "/",
          page: () => GetLoginPage(),
        ),
        GetPage(
          name: "/register",
          page: () => GetRegisterPage(),
        ),
      ],
      initialRoute: "/",
      navigatorObservers: [
        MyNavigatorObserver(),
      ],
    );
  }
}

class GetLoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("login"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("register"),
          onPressed: () => Get.toNamed("/register"),
        ),
      ),
    );
  }
}

class GetRegisterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("register"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("back"),
          onPressed: () => Get.back(),
        ),
      ),
    );
  }
}

To Reproduce Steps to reproduce the behavior:

  1. Run app on google chrome
  2. Click on 'register'
  3. Hot reload
  4. Click on 'back'

Expected behavior If i navigate through some routes and hot reload my app, i should be able to go back to a previous route, like with flutters navigator.

Flutter Version: Flutter 1.24.0-10.2.pre • channel beta

Getx Version: 3.21.2

Describe on which device you found the bug: Google Chrome Version 87.0.4280.66

Minimal reproduce code

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() {
  runApp(GetMyApp());
}

class GetMyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Get Demo',
      getPages: [
        GetPage(
          name: "/",
          page: () => GetLoginPage(),
        ),
        GetPage(
          name: "/register",
          page: () => GetRegisterPage(),
        ),
      ],
      initialRoute: "/",
      navigatorObservers: [
        MyNavigatorObserver(),
      ],
    );
  }
}

class GetLoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("login"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("register"),
          onPressed: () => Get.toNamed("/register"),
        ),
      ),
    );
  }
}

class GetRegisterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("register"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("back"),
          onPressed: () => Get.back(),
        ),
      ),
    );
  }
}

dakr0013 avatar Dec 02 '20 17:12 dakr0013

Unfortunately this is a recurring problem with Flutter web, all variables are cleared in the hotreload (not just those related to navigation), and for this reason their controllers are being destroyed. We could implement some feature to get around this in the routes, easily, but it would still affect controllers. You can see the related problem here:

https://github.com/flutter/flutter/issues/59277

jonataslaw avatar Dec 02 '20 18:12 jonataslaw

But if i use Navigator.pushNamed and MaterialApp the navigation stack does not get lost (see example). On hot reload, all pages get pushed on the stack again (you can see it in the console). If GetMaterialApp with Get.toNamed had the same behavior it could solve my problem. i am using bindings, so pushing the previous routes would create the controllers again.

dakr0013 avatar Dec 03 '20 06:12 dakr0013

Is there updates here?

hatemragab avatar Sep 17 '22 22:09 hatemragab

any update ?

Peshawa-k-kamil avatar Oct 01 '22 12:10 Peshawa-k-kamil

I am also facing the same issue, on the refresh screen from the chrome web browser, it removes the controller.

altafkhan8719 avatar Dec 20 '22 07:12 altafkhan8719

any updates?

rajeshbdabhi avatar Mar 01 '23 06:03 rajeshbdabhi

am also facing same issue

sinnoorc avatar Jun 21 '23 04:06 sinnoorc

Waiting for update too.

Messhias avatar Sep 22 '23 22:09 Messhias

Same.

SnowyTusk avatar Sep 26 '23 15:09 SnowyTusk

+1

wildsurfer avatar Dec 04 '23 07:12 wildsurfer

Same, GetX needs this feature to fully support the web, else the refresh button causes a mess because it causes the app to hot reload. Also, the navigation stack is messed up if you click the browser's forward button after refreshing.

MenaRaafat avatar Dec 27 '23 09:12 MenaRaafat

I think it is definitely fixed in version 5

jonataslaw avatar Dec 29 '23 02:12 jonataslaw

In future this issue will be solved.

aditya-wappnet avatar Jan 30 '24 13:01 aditya-wappnet

wainting for this fix

edrianolima avatar Feb 07 '24 12:02 edrianolima

Same here

dhcracchiolo avatar Mar 17 '24 21:03 dhcracchiolo

Any updates?

Henriquek47 avatar May 21 '24 22:05 Henriquek47

So, hope is the last thing to die! Waiting for a fix too.

jpedrosouza avatar Jun 13 '24 08:06 jpedrosouza

Any updates?

c-info avatar Jul 02 '24 07:07 c-info