@@ -6,7 +6,6 @@ import 'package:meta/meta.dart';
6
6
import 'package:ui/ui.dart' as ui;
7
7
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
8
8
9
- import '../dom.dart' ;
10
9
import '../platform_dispatcher.dart' ;
11
10
import '../services/message_codec.dart' ;
12
11
import '../services/message_codecs.dart' ;
@@ -257,20 +256,30 @@ class SingleEntryBrowserHistory extends BrowserHistory {
257
256
258
257
_setupStrategy (strategy);
259
258
260
- final String path = currentPath;
261
- if (! _isFlutterEntry (domWindow.history.state )) {
259
+ _currentRouteName = currentPath;
260
+ if (! _isFlutterEntry (currentState )) {
262
261
// An entry may not have come from Flutter, for example, when the user
263
262
// refreshes the page. They land directly on the "flutter" entry, so
264
263
// there's no need to set up the "origin" and "flutter" entries, we can
265
264
// safely assume they are already set up.
266
265
_setupOriginEntry (strategy);
267
- _setupFlutterEntry (strategy, path : path );
266
+ _setupFlutterEntry (strategy);
268
267
}
269
268
}
270
269
271
270
@override
272
271
final ui_web.UrlStrategy ? urlStrategy;
273
272
273
+ /// The route name of the current page.
274
+ ///
275
+ /// This is updated whenever the framework calls `setRouteName` . This is then
276
+ /// used when the user hits the back button to pop a nameless route, to restore
277
+ /// the route name from before the nameless route was pushed.
278
+ ///
279
+ /// This is also used to track the user-provided url when they change it
280
+ /// directly in the address bar.
281
+ String _currentRouteName = '/' ;
282
+
274
283
static const MethodCall _popRouteMethodCall = MethodCall ('popRoute' );
275
284
static const String _kFlutterTag = 'flutter' ;
276
285
static const String _kOriginTag = 'origin' ;
@@ -303,11 +312,11 @@ class SingleEntryBrowserHistory extends BrowserHistory {
303
312
@override
304
313
void setRouteName (String ? routeName, {Object ? state, bool replace = false }) {
305
314
if (urlStrategy != null ) {
306
- _setupFlutterEntry (urlStrategy! , replace: true , path: routeName);
315
+ _currentRouteName = routeName ?? currentPath;
316
+ _setupFlutterEntry (urlStrategy! , replace: true );
307
317
}
308
318
}
309
319
310
- String ? _userProvidedRouteName;
311
320
@override
312
321
void onPopState (Object ? state) {
313
322
if (_isOriginEntry (state)) {
@@ -323,17 +332,13 @@ class SingleEntryBrowserHistory extends BrowserHistory {
323
332
// We get into this scenario when the user changes the url manually. It
324
333
// causes a new entry to be pushed on top of our "flutter" one. When this
325
334
// happens it first goes to the "else" section below where we capture the
326
- // path into `_userProvidedRouteName ` then trigger a history back which
335
+ // path into `_currentRouteName ` then trigger a history back which
327
336
// brings us here.
328
- assert (_userProvidedRouteName != null );
329
-
330
- final String newRouteName = _userProvidedRouteName! ;
331
- _userProvidedRouteName = null ;
332
337
333
338
// Send a 'pushRoute' platform message so the app handles it accordingly.
334
339
EnginePlatformDispatcher .instance.invokeOnPlatformMessage (
335
340
'flutter/navigation' ,
336
- const JSONMethodCodec ().encodeMethodCall (MethodCall ('pushRoute' , newRouteName )),
341
+ const JSONMethodCodec ().encodeMethodCall (MethodCall ('pushRoute' , _currentRouteName )),
337
342
(_) {},
338
343
);
339
344
} else {
@@ -342,7 +347,7 @@ class SingleEntryBrowserHistory extends BrowserHistory {
342
347
// example.
343
348
344
349
// 1. We first capture the user's desired path.
345
- _userProvidedRouteName = currentPath;
350
+ _currentRouteName = currentPath;
346
351
347
352
// 2. Then we remove the new entry.
348
353
// This will take us back to our "flutter" entry and it causes a new
@@ -360,13 +365,9 @@ class SingleEntryBrowserHistory extends BrowserHistory {
360
365
361
366
/// This method is used manipulate the Flutter Entry which is always the
362
367
/// active entry while the Flutter app is running.
363
- void _setupFlutterEntry (ui_web.UrlStrategy strategy, {bool replace = false , String ? path}) {
364
- path ?? = currentPath;
365
- if (replace) {
366
- strategy.replaceState (_flutterState, 'flutter' , path);
367
- } else {
368
- strategy.pushState (_flutterState, 'flutter' , path);
369
- }
368
+ void _setupFlutterEntry (ui_web.UrlStrategy strategy, {bool replace = false }) {
369
+ final updateState = replace ? strategy.replaceState : strategy.pushState;
370
+ updateState (_flutterState, 'flutter' , _currentRouteName);
370
371
}
371
372
372
373
@override
0 commit comments