dart 如何在Flutter中使用go_router弹出初始路由?

tct7dpnv  于 2023-10-13  发布在  Flutter
关注(0)|答案(1)|浏览(128)

我正在探索go_router作为我的Flutter应用程序的导航解决方案,并面临一个我正在努力解决的简单场景。
我有初始路由/home导航屏幕到主页。在HomePage中,我有一个关闭按钮,可以简单地关闭HomePage,它是屏幕的第一个堆栈。
我希望我可以做一些与我使用标准Navigator所做的完全相同的事情:Navigator.of(context).pop()将关闭整个应用程序,因为我在路由的第一个堆栈中。
但是当我调用context.pop()时,它给了我以下错误:

======== Exception caught by gesture ===============================================================
The following GoError was thrown while handling a gesture:
There is nothing to pop

When the exception was thrown, this was the stack: 
#0      GoRouterDelegate.pop (package:go_router/src/delegate.dart:94:5)
#1      GoRouter.pop (package:go_router/src/router.dart:492:20)
#2      GoRouterHelper.pop (package:go_router/src/misc/extensions.dart:71:65)
#3      HomeView.build.<anonymous closure> (package:navigation/home_view.dart:25:33)
#4      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1096:21)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24)
#6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:627:11)
#7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:306:5)
#8      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:276:7)
#9      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:163:27)
#10     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:464:20)
#11     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:440:22)
#12     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:336:11)
#13     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:395:7)
#14     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:357:5)
#15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:314:7)
#16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:295:7)
#17     _invoke1 (dart:ui/hooks.dart:164:13)
#18     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:361:7)
#19     _dispatchPointerDataPacket (dart:ui/hooks.dart:91:31)
Handler: "onTap"
Recognizer: TapGestureRecognizer#94750
  debugOwner: GestureDetector
  state: ready
  won arena
  finalPosition: Offset(165.4, 119.6)
  finalLocalPosition: Offset(31.6, 25.6)
  button: 1
  sent tap down
====================================================================================================

如何像在go_router中使用Navigator.of(context).pop()一样关闭应用?

tkclm6bt

tkclm6bt1#

您可以使用SystemNavigator来处理应用程序导航堆栈

import 'package:flutter/services.dart';

SystemNavigator.pop();

相关问题