处理后无法安装 MaterialPageRoute<void>
Cannot install a MaterialPageRoute<void> after disposing it
设置
我必须走简单的路线,FirstRoute
和 SecondRoute
。每个都包含一个用于导航的按钮。按 FirstRoute
的按钮会转到第二个,反之亦然。
异常
当我尝试 第二次 从 FirstRoute
导航到 SecondRoute
时出现异常(第一次尝试成功)。根据消息“处理后无法安装 MaterialPageRoute ”,我以某种方式处理了一条路线,但我不知道是哪条路线以及我是如何做到的。
这是完整的错误日志:
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (26569): The following assertion was thrown while handling a gesture:
I/flutter (26569): Cannot install a MaterialPageRoute<void> after disposing it.
I/flutter (26569): 'package:flutter/src/widgets/routes.dart': Failed assertion: line 175 pos 12:
I/flutter (26569): '!_transitionCompleter.isCompleted'
I/flutter (26569): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (26569): more information in this error message to help you determine and fix the underlying cause.
I/flutter (26569): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (26569): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (26569): When the exception was thrown, this was the stack:
I/flutter (26569): #2 TransitionRoute.install (package:flutter/src/widgets/routes.dart:175:12)
I/flutter (26569): #3 ModalRoute.install (package:flutter/src/widgets/routes.dart:895:11)
I/flutter (26569): #4 NavigatorState.push (package:flutter/src/widgets/navigator.dart:1742:11)
I/flutter (26569): #5 Navigator.push (package:flutter/src/widgets/navigator.dart:1081:34)
I/flutter (26569): #6 _createOnPressed.<anonymous closure> (package:layout_tutorial/main.dart:46:32)
I/flutter (26569): #7 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
I/flutter (26569): #8 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
I/flutter (26569): #9 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)
I/flutter (26569): #10 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:246:9)
I/flutter (26569): #11 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
I/flutter (26569): #12 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:436:9)
I/flutter (26569): #13 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
I/flutter (26569): #14 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
I/flutter (26569): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:214:19)
I/flutter (26569): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
I/flutter (26569): #17 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
I/flutter (26569): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
I/flutter (26569): #19 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
I/flutter (26569): #23 _invoke1 (dart:ui/hooks.dart:223:10)
I/flutter (26569): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
I/flutter (26569): (elided 5 frames from class _AssertionError and package dart:async)
I/flutter (26569): Handler: onTap
I/flutter (26569): Recognizer:
I/flutter (26569): TapGestureRecognizer#1bc15(debugOwner: GestureDetector, state: possible, won arena, finalPosition:
I/flutter (26569): Offset(164.3, 365.3), sent tap down)
I/flutter (26569): ════════════════════════════════════════════════════════════════════════════════════════════════════
这里是完整的代码,以防有人想重现错误:
import 'package:flutter/material.dart';
typedef void OnPressed();
main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: FirstRoute(),
));
}
class FirstRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return _getRouteScaffold('First route', 'Go to second route',
_createOnPressed(context, route: SecondRoute()));
}
}
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return _getRouteScaffold(
'Second route', 'Back to first route', _createOnPressed(context));
}
}
Widget _getRouteScaffold(String title, String buttonText, OnPressed function) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: RaisedButton(
child: Text(buttonText),
onPressed: function,
)),
);
}
OnPressed _createOnPressed(BuildContext context, {StatelessWidget route}) {
OnPressed function;
if (route != null) {
MaterialPageRoute<void> materialRoute =
MaterialPageRoute<void>(builder: (context) => route);
function = () => Navigator.push(context, materialRoute);
} else {
function = () => Navigator.pop(context);
}
return function;
}
恕我直言,所有这些方法都不是最佳实践,但我确信构建此方法及其构建方式是有原因的。无论如何,我在 第 16 行 :
中发现了错误
_createOnPressed(context, route: SecondRoute()));
您将此函数传递给按钮,它将始终以 相同 SecondRoute
执行。 MaterialPageRoute 被销毁后,就不能再使用了。因此,您必须 每次 建立一条新路线。
要做到这一点,您可以简单地将您的方法调用扭曲到另一个闭包中,然后在按下时执行,并启动一个新实例。我只是将第 16 行调整为 修复 你的错误:
() => _createOnPressed(context, route: SecondRoute())());
干杯:)
设置
我必须走简单的路线,FirstRoute
和 SecondRoute
。每个都包含一个用于导航的按钮。按 FirstRoute
的按钮会转到第二个,反之亦然。
异常
当我尝试 第二次 从 FirstRoute
导航到 SecondRoute
时出现异常(第一次尝试成功)。根据消息“处理后无法安装 MaterialPageRoute ”,我以某种方式处理了一条路线,但我不知道是哪条路线以及我是如何做到的。
这是完整的错误日志:
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (26569): The following assertion was thrown while handling a gesture:
I/flutter (26569): Cannot install a MaterialPageRoute<void> after disposing it.
I/flutter (26569): 'package:flutter/src/widgets/routes.dart': Failed assertion: line 175 pos 12:
I/flutter (26569): '!_transitionCompleter.isCompleted'
I/flutter (26569): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (26569): more information in this error message to help you determine and fix the underlying cause.
I/flutter (26569): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (26569): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (26569): When the exception was thrown, this was the stack:
I/flutter (26569): #2 TransitionRoute.install (package:flutter/src/widgets/routes.dart:175:12)
I/flutter (26569): #3 ModalRoute.install (package:flutter/src/widgets/routes.dart:895:11)
I/flutter (26569): #4 NavigatorState.push (package:flutter/src/widgets/navigator.dart:1742:11)
I/flutter (26569): #5 Navigator.push (package:flutter/src/widgets/navigator.dart:1081:34)
I/flutter (26569): #6 _createOnPressed.<anonymous closure> (package:layout_tutorial/main.dart:46:32)
I/flutter (26569): #7 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
I/flutter (26569): #8 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
I/flutter (26569): #9 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)
I/flutter (26569): #10 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:246:9)
I/flutter (26569): #11 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
I/flutter (26569): #12 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:436:9)
I/flutter (26569): #13 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
I/flutter (26569): #14 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
I/flutter (26569): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:214:19)
I/flutter (26569): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
I/flutter (26569): #17 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
I/flutter (26569): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
I/flutter (26569): #19 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
I/flutter (26569): #23 _invoke1 (dart:ui/hooks.dart:223:10)
I/flutter (26569): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
I/flutter (26569): (elided 5 frames from class _AssertionError and package dart:async)
I/flutter (26569): Handler: onTap
I/flutter (26569): Recognizer:
I/flutter (26569): TapGestureRecognizer#1bc15(debugOwner: GestureDetector, state: possible, won arena, finalPosition:
I/flutter (26569): Offset(164.3, 365.3), sent tap down)
I/flutter (26569): ════════════════════════════════════════════════════════════════════════════════════════════════════
这里是完整的代码,以防有人想重现错误:
import 'package:flutter/material.dart';
typedef void OnPressed();
main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: FirstRoute(),
));
}
class FirstRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return _getRouteScaffold('First route', 'Go to second route',
_createOnPressed(context, route: SecondRoute()));
}
}
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return _getRouteScaffold(
'Second route', 'Back to first route', _createOnPressed(context));
}
}
Widget _getRouteScaffold(String title, String buttonText, OnPressed function) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: RaisedButton(
child: Text(buttonText),
onPressed: function,
)),
);
}
OnPressed _createOnPressed(BuildContext context, {StatelessWidget route}) {
OnPressed function;
if (route != null) {
MaterialPageRoute<void> materialRoute =
MaterialPageRoute<void>(builder: (context) => route);
function = () => Navigator.push(context, materialRoute);
} else {
function = () => Navigator.pop(context);
}
return function;
}
恕我直言,所有这些方法都不是最佳实践,但我确信构建此方法及其构建方式是有原因的。无论如何,我在 第 16 行 :
中发现了错误_createOnPressed(context, route: SecondRoute()));
您将此函数传递给按钮,它将始终以 相同 SecondRoute
执行。 MaterialPageRoute 被销毁后,就不能再使用了。因此,您必须 每次 建立一条新路线。
要做到这一点,您可以简单地将您的方法调用扭曲到另一个闭包中,然后在按下时执行,并启动一个新实例。我只是将第 16 行调整为 修复 你的错误:
() => _createOnPressed(context, route: SecondRoute())());
干杯:)