浏览器和应用栏中的后退按钮在 Flutter Web 开发中不起作用

Posted

技术标签:

【中文标题】浏览器和应用栏中的后退按钮在 Flutter Web 开发中不起作用【英文标题】:Back button in browser and app bar won't work in Flutter web development 【发布时间】:2020-07-28 20:21:11 【问题描述】:

我是 Flutter 的新手,在我的 Web 应用程序上进行页面导航时遇到困难,特别是当我从第 1 页导航到第 2 页时,每当我尝试返回时(无论是浏览器中的后退按钮还是浏览器中的后退按钮)自动创建的应用栏),我得到一个 IndexError 并且应用程序崩溃。我正在尝试使用 Navigator.push,但这似乎不起作用。

RangeError (index): Index out of range: index should be less than 6: 6

When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 216:49  throw_
dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart 577:7             _get]
packages/booth_v1/main.dart 93:70                                             <fn>
packages/flutter/src/widgets/sliver.dart 446:22                               build
packages/flutter/src/widgets/sliver.dart 1134:67                              <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/identity_hash_map.dart 77:23    putIfAbsent
packages/flutter/src/widgets/sliver.dart 1134:26                              [_build]
packages/flutter/src/widgets/sliver.dart 1147:55                              <fn>
packages/flutter/src/widgets/framework.dart 2607:19                           buildScope
packages/flutter/src/widgets/sliver.dart 1140:11                              createChild
packages/flutter/src/rendering/sliver_multi_box_adaptor.dart 354:23           <fn>
packages/flutter/src/rendering/object.dart 1866:58                            <fn>
packages/flutter/src/rendering/object.dart 918:15                             [_enableMutationsToDirtySubtrees]
packages/flutter/src/rendering/object.dart 1866:13                            invokeLayoutCallback
packages/flutter/src/rendering/sliver_multi_box_adaptor.dart 343:5            [_createOrObtainChild]
packages/flutter/src/rendering/sliver_multi_box_adaptor.dart 489:5            insertAndLayoutChild
packages/flutter/src/rendering/sliver_grid.dart 587:17                        performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/sliver_padding.dart 135:11                     performLayout
packages/flutter/src/rendering/sliver_padding.dart 375:11                     performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/viewport.dart 452:12                           layoutChildSequence
packages/flutter/src/rendering/viewport.dart 1444:12                          [_attemptLayout]
packages/flutter/src/rendering/viewport.dart 1353:20                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/shifted_box.dart 394:13                        performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/custom_layout.dart 171:10                      layoutChild
packages/flutter/src/material/scaffold.dart 484:7                             performLayout
packages/flutter/src/rendering/custom_layout.dart 240:7                       [_callPerformLayout]
packages/flutter/src/rendering/custom_layout.dart 399:14                      performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/proxy_box.dart 1248:11                         performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/proxy_box.dart 3225:13                         performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/widgets/overlay.dart 700:14                              performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/proxy_box.dart 111:13                          performLayout
packages/flutter/src/rendering/object.dart 1767:7                             layout
packages/flutter/src/rendering/view.dart 167:48                               performLayout
packages/flutter/src/rendering/object.dart 1630:7                             [_layoutWithoutResize]
packages/flutter/src/rendering/object.dart 887:17                             flushLayout
packages/flutter/src/rendering/binding.dart 402:19                            drawFrame
packages/flutter/src/widgets/binding.dart 865:13                              drawFrame
packages/flutter/src/rendering/binding.dart 284:5                             [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1074:15                           [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1013:9                            handleDrawFrame
packages/flutter/src/scheduler/binding.dart 822:7                             <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 50:19       internalCallback
════════════════════════════════════════════════════════════════════════════════════════════════════
Another exception was thrown: RangeError (index): Index out of range: index should be less than 6: 7
Another exception was thrown: Assertion failed: org-dartlang-sdk:///flutter_web_sdk/lib/_engine/engine/surface/clip.dart:289:12
Another exception was thrown: PersistedScene: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedPhysicalShape: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.
Another exception was thrown: PersistedOffset: is in an unexpected state.

这是我的代码示例

@override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        iconTheme: new IconThemeData(color: Colors.black),
        title: Text(
          widget.title,
          style: TextStyle(
            color: Colors.black,
            fontSize: 35,
            fontWeight: FontWeight.w400
          ),
        ),
        actions: [
          new IconButton(icon: Icon(Icons.shopping_cart,color: Colors.black), onPressed: () 
            Navigator.push(context,MaterialPageRoute(builder: (BuildContext context) => Cart()));
          )
        ],
        backgroundColor: Colors.white,
        centerTitle: true,
        elevation: 0.0,
      ),

【问题讨论】:

【参考方案1】:

Navigator.push(context,MaterialPageRoute(builder: (context) => Cart()));

替换它

【讨论】:

以上是关于浏览器和应用栏中的后退按钮在 Flutter Web 开发中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何摆脱Android应用栏中的后退按钮

无法删除 sliver 应用栏中的后退按钮

Android - 标题栏中的后退按钮

Flutter Web 中的路由有意外行为

如何在底部应用栏中使用后退按钮实现导航。安卓

从 SwiftUI 中的导航栏中删除后退按钮文本