简体   繁体   English

无法导航到 flutter 中的主页

[英]Unable to navigate to home page in flutter

I have an app comprising of home and update screens.我有一个包含主屏幕和更新屏幕的应用程序。

I am unable to navigate back to the home screen from the update screen.我无法从更新屏幕导航回主屏幕。

See below code for home screen请参阅下面的主屏幕代码

  // build the list widget
  Widget _buildTaskWidget(task) {
    return ListTile(
      leading: Icon(Icons.assignment),
      title: Text(task['name']),
      subtitle: Text(task['created_at']),
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => UpdateTask(task: task),
          ),
        );
      }
    );
  }

See below code for the update screen请参阅下面的更新屏幕代码

@override
      Widget build(BuildContext context) {
        // final Task task = ModalRoute.of(context).settings.arguments;
        return Scaffold(
          resizeToAvoidBottomInset: true,
          appBar: AppBar(
            title: Text('Update Task'),
          ),
          body: ListView(
            children: <Widget>[
              inputWidget(),
              inputWidgetForVendor(),
              inputWidgetForAmount(),
              Container(
                margin: EdgeInsets.fromLTRB(45, 1, 45, 1),
                child: RaisedButton(
                  color: Colors.blueAccent,
                  child: Text('Update Task', style: TextStyle(color: Colors.white)),
                  onPressed: () async {
                     var res = await updateNewTask(_taskTextInput.text, _vendorTextInput.text,  _amountTextInput.text, id);
                     print(res);
                      Navigator.pop(context);
                  },
                ),
              )
            ],
          )// This trailing comma makes auto-formatting nicer for build methods.
        );
      }

If I remove the current onPressed function and replace with this below, it works如果我删除当前的 onPressed function 并替换为下面的,它可以工作

onPressed: () { Navigator.pop(context); },

What am I doing wrong in the initial function?我在最初的 function 中做错了什么? The update function successfully updates the list items, however I am unable to navigate back.更新 function 成功更新了列表项,但是我无法返回。 See below error logs:请参阅以下错误日志:

E/flutter (27123): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'
E/flutter (27123): #0      updateNewTask (package:task/repository/services.dart:67:10)
E/flutter (27123): <asynchronous suspension>

Please help.请帮忙。

Maybe outsourcing your async update function is a simple solution at this point, when you want to instantly go back to your home screen.当您想立即将 go 返回主屏幕时,也许外包您的异步更新 function 是一个简单的解决方案。 You could print the update directly in the function then.然后,您可以直接在 function 中打印更新。

Just leave onpressed() as it is.只需保持 onpressed() 不变。

onPressed: () {
  updateNewTask(_taskTextInput.text, _vendorTextInput.text,  _amountTextInput.text, id);
  Navigator.pop(context);
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM