颤动:使用navigator.dart

时间:2018-05-01 13:57:46

标签: dart flutter

我是Flutter的新手并且玩弄它。所以,请耐心等待我。

单击PopupMenuButton的特定菜单项时会抛出以下异常,但始终只是第二次:

  

'包:flutter / src / widgets / navigator.dart':失败的断言:行   1846 pos 12:'!_ debugLocked':不是真的。

这里的设置:

用于指定已定义类之后的菜单项:

class PopupMenuChoice {
  const PopupMenuChoice({this.title, this.pageRoute});

  final String title;
  final MaterialPageRoute pageRoute;
}

AppBar的actions属性中PopupMenuButton的定义:

new PopupMenuButton<PopupMenuChoice>(
    itemBuilder: (BuildContext context) {
      return _popupMenus.map((PopupMenuChoice choice) {
        return new PopupMenuItem<PopupMenuChoice>(
          value: choice,
          child: new Text(choice.title),
        );
      }).toList();
    },
    onSelected: _popupMenuSelected,
),

相应的小部件在下面的类中定义(AppBar在&#34中创建;返回新的脚手架&#34;此类):

class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];
  final _saved = new Set<WordPair>();
  final _popupMenus = <PopupMenuChoice>[];

  ...
}

正如您所看到的,有用于保存WordPair对象的私有变量,还有用于菜单选择的私有变量。

_popupMenus列表在&#34;构建覆盖&#34;中设置:

@override
Widget build(BuildContext context) {
    // Setup page routes
    if (_popupMenus.where((p) => p.title == 'Saved Suggestions').length == 0) {
      final _pageRouteSavedSuggestions = new MaterialPageRoute(
        builder: (context) {
          final tiles = _saved.map(
            (pair) {
              return new ListTile(
                title: new Text(
                  pair.asPascalCase,
                  style: _biggerFont,
                ),
              );
            },
          );
          final divided = ListTile
              .divideTiles(
                context: context,
                tiles: tiles,
              )
              .toList();

          return new Scaffold(
            appBar: new AppBar(
              title: new Text('Saved Suggestions'),
            ),
            body: new ListView(children: divided),
          );
        },
      );
      _popupMenus.add(new PopupMenuChoice(
          title: 'Saved Suggestions', pageRoute: _pageRouteSavedSuggestions));
    }

    if (_popupMenus.where((p) => p.title == 'TEST Page').length == 0) {
      final _pageRouteTest = new MaterialPageRoute(
        builder: (context) {
          return new Scaffold(
            appBar: new AppBar(
              title: new Text('TEST Page'),
            ),
            body: new Text('Some content...'),
          );
        },
      );
      _popupMenus.add(
          new PopupMenuChoice(title: 'TEST Page', pageRoute: _pageRouteTest));
    }
    ...

在PopupMenuChoice的定义的MaterialPageRoute中,私有变量可能是访问权限(例如_saved)。

这里是onSelected of PopupMenuButton的相应事件处理程序:

void _popupMenuSelected(PopupMenuChoice choice) {
  Navigator.of(context).push(choice.pageRoute);
}

有人可以解释为什么会抛出这个异常吗?怎么可以预防?

谢谢, 罗杰

在特定菜单项上单击第二次时来自调试控制台的其他信息:

  

E / flutter(17133):[错误:topaz / lib / tonic / logging / dart_error.cc(16)]   未处理的异常:E / flutter(17133):   &#39;包:flutter / src / widgets / routes.dart&#39;:失败的断言:第177行   pos 12:&#39;!_ transitionCompleter.isCompleted&#39;:无法安装   处理后的MaterialPageRoute。 E / flutter(17133):#0
  _AssertionError._doThrowNew(dart:core / runtime / liberrors_patch.dart:37:39)E / flutter(17133):#1
  _AssertionError._throwNew(dart:core / runtime / liberrors_patch.dart:33:5)E / flutter(17133):#2
  TransitionRoute.install(包:flutter / src / widgets / routes.dart)   E / flutter(17133):#3 ModalRoute.install   (包:flutter / src / widgets / routes.dart:740:11)E / flutter(17133):#4   NavigatorState.push   (包:flutter / src / widgets / navigator.dart:1444:11)E / flutter   (17133):#5 RandomWordsState.build._popupMenuSelected   (文件:/// d:/Flutter%20Projects/startup_namer/lib/main.dart:166:29)   E / flutter(17133):#6
  _PopupMenuButtonState.showButtonMenu。 (包:flutter / src / material / popup_menu.dart)E / flutter(17133):#7
  _RootZone.runUnary(dart:async / zone.dart:1381:54)E / flutter(17133):#8 _FutureListener.handleValue(dart:async / future_impl.dart:129:18)E / flutter(17133):#9
  Future._propagateToListeners.handleValueCallback   (dart:async / future_impl.dart:633:45)E / flutter(17133):#10
  Future._propagateToListeners(dart:async / future_impl.dart:662:32)   E / flutter(17133):#11 Future._completeWithValue   (dart:async / future_impl.dart:477:5)E / flutter(17133):#12
  Future._asyncComplete。   (dart:async / future_impl.dart:507:7)E / flutter(17133):#13
  _microtaskLoop(dart:async / schedule_microtask.dart:41:21)E / flutter(17133):#14 _startMicrotaskLoop   (镖:异步/ schedule_microtask.dart:50:5)

3 个答案:

答案 0 :(得分:1)

您尝试过吗:

void _popupMenuSelected(PopupMenuChoice choice) {
  Navigator.push(context, choice.pageRoute);
}

答案 1 :(得分:0)

    void _popupMenuSelected(PopupMenuChoice choice) {
      await Future.delayed(const Duration(milliseconds: 100));
      Navigator.push(context, choice.pageRoute);
    }

答案 2 :(得分:0)

假设您有Auth Block, 那里你有 isLoggedIn = false; 以及发生登录网络呼叫的方法:

假设您设计这样的方法:

logIn(String userId,String Passwrod){
 url ....

if(response.body == 200){
isLoggedIn = true ;
return response.body;
}
}

现在在您的Auth页面中:问题是:

在请求页面类时,您具有检查器:

(isLoggedIn){
Navigator.push(context, choice.pageRoute);
}

并且当用户单击“提交”按钮时,会出现以下内容:

(){
Navigator.push(context, choice.pageRoute);

}

现在的问题是您同时被调用,这意味着LoggedIn = true,然后此处的提交按钮响应就是问题。 删除首字母

(isLoggedIn){
Navigator.push(context, choice.pageRoute);
}

您的问题将得到解决。

将此检查器用于登录页面以外的其他页面