3

设置

我必须简单的路线,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;
}
4

1 回答 1

2

所有这些方法都不是最佳实践,但我确信构建它是有原因的,它的构建方式。无论如何,我在第 16 行发现了错误:

_createOnPressed(context, route: SecondRoute()));

您将此函数传递给按钮,该按钮将始终以相同 SecondRoute的. MaterialPageRoute被释放后,就不能再使用了。因此,您每次都必须建立一条新路线

为此,您可以简单地在另一个闭包中扭曲您的方法调用,然后在按下时执行该闭包,并启动一个新实例。我只是调整了第 16 行来修复您的错误:

() => _createOnPressed(context, route: SecondRoute())());

干杯:)

于 2019-03-29T12:34:11.113 回答