用户登录后在StoreConnector中颤振Redux Navigator.pop()

时间:2020-04-27 11:20:33

标签: flutter redux flutter-redux

我该怎么做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)
...

我在这里写的原因是,由于我是计算机化的,所以我什至不知道如何在Google上进行查询(我处理react)

2 个答案:

答案 0 :(得分:2)

理想情况下,您不应在小部件树中调用导航器,否则会出现错误。

为此,StoreConnector使用了onWillChange方法。在构建窗口小部件之前,此功能将在状态更改时运行。 (类似于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 Link

答案 1 :(得分:0)

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

添加此方法。

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

按以下方式更改条件。

if(viewModel.user.email != '') {
      changenavigation();
       return Container();
    }