1

我该怎么做Navigator.of(context).pop((route) => false); 在我的登录屏幕中

class _LoginState extends State<Login> with SingleTickerProviderStateMixin {
  String email = '';
  String password = '';
  final Store<AppState> store;
  final _formKey = GlobalKey<FormState>();

  _LoginState(this.store);


  @override
  Widget build(BuildContext context) {
    return new StoreConnector<AppState, LoginViewModel>(
      converter: ((Store<AppState> store) => LoginViewModel.create(store)),
      builder: (BuildContext context, LoginViewModel viewModel)  {
        if(viewModel.user.email != '') {
           Navigator.of(context).pop((route) => false);
           return null;
        }
        return (
          viewModel.isLoading ? 
            Center(...)
            :
            Scaffold(...)

这有效,但显示错误。

Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4167:11)
I/flutter (13435): #1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4182:6)
I/flutter (13435): #2      State.setState (package:flutter/src/widgets/framework.dart:1253:14)
...

我写在这里是因为我什至不知道如何在谷歌上制定一个查询,因为我是电脑化的 Flutter(我处理反应)

4

2 回答 2

2

理想情况下,您不应在小部件树中调用 Navigator,因此您会收到错误消息。

这样做是StoreConnectoronWillChange方法的。在构建 Widget 之前,它是一个将在状态更改时运行的函数。(类似于 react 类组件中的 componentWillUpdate)

class _LoginState extends State<Login> with SingleTickerProviderStateMixin {
  String email = '';
  String password = '';
  final Store<AppState> store;
  final _formKey = GlobalKey<FormState>();

  _LoginState(this.store);


  @override
  Widget build(BuildContext context) {
    return new StoreConnector<AppState, LoginViewModel>(
      converter: ((Store<AppState> store) => LoginViewModel.create(store)),
      onWillChange: (LoginViewModel prev, LoginViewModel viewModel) {
        if(viewModel.user.email != '') {
           Navigator.of(context).pop((route) => false);
           return null;
        }
      },
      builder: (BuildContext context, LoginViewModel viewModel)  {
        return (
          viewModel.isLoading ? 
            Center(...)
            :
            Scaffold(...)

您可以在源代码中探索 StoreConnector 公开的其他方法。(Github 链接

于 2020-04-27T14:04:05.933 回答
0

我认为问题在于您想在构建屏幕时导航,这是不可能的。

添加这个方法。

changenavigation() async {
    await Future.delayed(Duration(microseconds: 1));
       Navigator.of(context).pop((route) => false);
  }

按以下方式更改条件。

if(viewModel.user.email != '') {
      changenavigation();
       return Container();
    }
于 2020-04-27T12:06:16.923 回答