flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
164.83k stars 27.16k forks source link

[go_router] Page 1 loses state after triggering router redirection #153916

Closed Zia-Ch closed 2 weeks ago

Zia-Ch commented 3 weeks ago

What package does this bug report belong to?

go_router

What target platforms are you seeing this bug on?

Android, Web, Windows

Have you already upgraded your packages?

Yes

Dependency versions

pubspec.lock ```lock # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: args: dependency: transitive description: name: args sha256: "7cf60b9f0cc88203c5a190b4cd62a99feea42759a7fa695010eb5de1c0b2252a" url: "https://pub.dev" source: hosted version: "2.5.0" async: dependency: transitive description: name: async sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" url: "https://pub.dev" source: hosted version: "2.11.0" boolean_selector: dependency: transitive description: name: boolean_selector sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" url: "https://pub.dev" source: hosted version: "2.1.1" characters: dependency: transitive description: name: characters sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" url: "https://pub.dev" source: hosted version: "1.3.0" clock: dependency: transitive description: name: clock sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf url: "https://pub.dev" source: hosted version: "1.1.1" collection: dependency: transitive description: name: collection sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted version: "1.18.0" connectivity_plus: dependency: "direct main" description: name: connectivity_plus sha256: "2056db5241f96cdc0126bd94459fc4cdc13876753768fc7a31c425e50a7177d0" url: "https://pub.dev" source: hosted version: "6.0.5" connectivity_plus_platform_interface: dependency: transitive description: name: connectivity_plus_platform_interface sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204" url: "https://pub.dev" source: hosted version: "2.0.1" cupertino_icons: dependency: "direct main" description: name: cupertino_icons sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6 url: "https://pub.dev" source: hosted version: "1.0.8" dbus: dependency: transitive description: name: dbus sha256: "365c771ac3b0e58845f39ec6deebc76e3276aa9922b0cc60840712094d9047ac" url: "https://pub.dev" source: hosted version: "0.7.10" fake_async: dependency: transitive description: name: fake_async sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" url: "https://pub.dev" source: hosted version: "1.3.1" ffi: dependency: transitive description: name: ffi sha256: "16ed7b077ef01ad6170a3d0c57caa4a112a38d7a2ed5602e0aca9ca6f3d98da6" url: "https://pub.dev" source: hosted version: "2.1.3" flutter: dependency: "direct main" description: flutter source: sdk version: "0.0.0" flutter_lints: dependency: "direct dev" description: name: flutter_lints sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c" url: "https://pub.dev" source: hosted version: "4.0.0" flutter_riverpod: dependency: "direct main" description: name: flutter_riverpod sha256: "0f1974eff5bbe774bf1d870e406fc6f29e3d6f1c46bd9c58e7172ff68a785d7d" url: "https://pub.dev" source: hosted version: "2.5.1" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" flutter_web_plugins: dependency: transitive description: flutter source: sdk version: "0.0.0" go_router: dependency: "direct main" description: name: go_router sha256: "48d03a1e4887b00fe622695139246e3c778ac814eeb32421467b56d23fa64034" url: "https://pub.dev" source: hosted version: "14.2.6" leak_tracker: dependency: transitive description: name: leak_tracker sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" url: "https://pub.dev" source: hosted version: "10.0.5" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" url: "https://pub.dev" source: hosted version: "3.0.5" leak_tracker_testing: dependency: transitive description: name: leak_tracker_testing sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" url: "https://pub.dev" source: hosted version: "3.0.1" lints: dependency: transitive description: name: lints sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235" url: "https://pub.dev" source: hosted version: "4.0.0" logging: dependency: transitive description: name: logging sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340" url: "https://pub.dev" source: hosted version: "1.2.0" matcher: dependency: transitive description: name: matcher sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec url: "https://pub.dev" source: hosted version: "0.11.1" meta: dependency: transitive description: name: meta sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 url: "https://pub.dev" source: hosted version: "1.15.0" nm: dependency: transitive description: name: nm sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254" url: "https://pub.dev" source: hosted version: "0.5.0" path: dependency: transitive description: name: path sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted version: "1.9.0" petitparser: dependency: transitive description: name: petitparser sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27 url: "https://pub.dev" source: hosted version: "6.0.2" plugin_platform_interface: dependency: transitive description: name: plugin_platform_interface sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" url: "https://pub.dev" source: hosted version: "2.1.8" riverpod: dependency: transitive description: name: riverpod sha256: f21b32ffd26a36555e501b04f4a5dca43ed59e16343f1a30c13632b2351dfa4d url: "https://pub.dev" source: hosted version: "2.5.1" sky_engine: dependency: transitive description: flutter source: sdk version: "0.0.99" source_span: dependency: transitive description: name: source_span sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" url: "https://pub.dev" source: hosted version: "1.10.0" stack_trace: dependency: transitive description: name: stack_trace sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted version: "1.11.1" state_notifier: dependency: transitive description: name: state_notifier sha256: b8677376aa54f2d7c58280d5a007f9e8774f1968d1fb1c096adcb4792fba29bb url: "https://pub.dev" source: hosted version: "1.0.0" stream_channel: dependency: transitive description: name: stream_channel sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted version: "2.1.2" string_scanner: dependency: transitive description: name: string_scanner sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" url: "https://pub.dev" source: hosted version: "1.2.0" term_glyph: dependency: transitive description: name: term_glyph sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84 url: "https://pub.dev" source: hosted version: "1.2.1" test_api: dependency: transitive description: name: test_api sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" url: "https://pub.dev" source: hosted version: "0.7.2" vector_math: dependency: transitive description: name: vector_math sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803" url: "https://pub.dev" source: hosted version: "2.1.4" vm_service: dependency: transitive description: name: vm_service sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc url: "https://pub.dev" source: hosted version: "14.2.4" web: dependency: transitive description: name: web sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062 url: "https://pub.dev" source: hosted version: "1.0.0" xml: dependency: transitive description: name: xml sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226 url: "https://pub.dev" source: hosted version: "6.5.0" sdks: dart: ">=3.5.0 <4.0.0" flutter: ">=3.19.0" ```

Steps to reproduce

Run the app If there is network connection app will go to home page. On home page I have a listview up to 100 elements. Now scroll to any element say 50. And now turn of your platform/system/mobile data connection. App will be redirected to No Network page. Now reconnect your network. Home page will show listview starting from 0 instead of where you left it at 50 as per our example.

Expected results

App should restore state of home page and show listview at element 50.

Actual results

Its is not restoring state of where home page was before redirection.

Code sample

Code sample ```dart import 'dart:async'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; final conectionProvider = StateNotifierProvider((ref) { return NetworkInfo(); }); class NetworkInfo extends StateNotifier { NetworkInfo() : super(true) { subscribeToConnection(); } late StreamSubscription> _subscription; subscribeToConnection() async { _subscription = Connectivity() .onConnectivityChanged .listen((List result) { final ConnectivityResult connected = result.first; if (connected != ConnectivityResult.none) { state = true; } else { state = false; } }); } @override void dispose() { _subscription.cancel(); super.dispose(); } } final goRouteProvider = Provider((ref) { final connected = ref.watch(conectionProvider); return GoRouter( debugLogDiagnostics: true, initialLocation: '/', routes: [ GoRoute( path: '/', builder: (BuildContext context, GoRouterState state) => const HomeScreen(), ), GoRoute( path: '/nonetwork', builder: (BuildContext context, GoRouterState state) => const LoginScreen(), ), ], redirect: (BuildContext context, GoRouterState state) { if (!connected) { return '/nonetwork'; } return null; }, ); }); void main() { runApp(const ProviderScope( child: App(), )); } class App extends ConsumerWidget { const App({super.key}); static const String title = 'GoRouter Example: Redirection'; @override Widget build(BuildContext context, WidgetRef ref) { final router = ref.watch(goRouteProvider); return MaterialApp.router( title: title, routerConfig: router, ); } } class LoginScreen extends StatelessWidget { const LoginScreen({super.key}); @override Widget build(BuildContext context) => Scaffold( appBar: AppBar(title: const Text(App.title)), body: const Center( child: Text('No network connection. Please try again later.')), ); } class HomeScreen extends StatelessWidget { const HomeScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text(App.title), ), body: Center( child: ListView.builder( itemCount: 100, itemBuilder: (BuildContext context, int index) => ListTile( title: Text('Item $index'), ), ), ), ); } } ```

Screenshots or Videos

Screenshots / Video demonstration

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

Doctor output ```console Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.24.0, on Microsoft Windows [Version 10.0.22621.4037], locale en-PK) [✓] Windows Version (Installed version of Windows is version 10 or higher) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Chrome - develop for the web [✓] Visual Studio - develop Windows apps (Visual Studio Build Tools 2022 17.6.5) [✓] Android Studio (version 2022.2) [✓] VS Code (version 1.92.2) [✓] Connected device (3 available) [✓] Network resources • No issues found! ```
huycozy commented 3 weeks ago

Reproduced this using the latest package version go_router: ^14.2.6 on Flutter stable 3.24.1. This is most likely having the same cause as https://github.com/flutter/flutter/issues/146616 and https://github.com/flutter/flutter/issues/123973.

chunhtai commented 2 weeks ago

This is expected, when you switch from / to /nonetwork, the / is removed from the navigator and thus destroyed. If you want to keep state across the entire app, you will need to store it in a state management system

github-actions[bot] commented 14 hours ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.