设置
我必须简单的路线,FirstRoute
并且SecondRoute
. 每个都包含一个用于导航的按钮。按FirstRoute
' 按钮导致第二个,反之亦然。
例外
当我第二次FirstRoute
尝试从to导航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;
}