【问题标题】:Blackscreen when navigate to new page flutter导航到新页面时出现黑屏
【发布时间】:2022-01-18 10:14:35
【问题描述】:

目前我创建了一个应用程序,我在列表中显示我的传感器数据,但我的问题是,如果我想导航到列表页面,我总是会得到一个黑屏。 我试着这样导航:

Container(
                  height: 150.0,
                  margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
                  child: Material(
                    borderRadius: BorderRadius.circular(20.0),
                    shadowColor: Colors.greenAccent,
                    color: Colors.green,
                    elevation: 7.0,
                    child: GestureDetector(
                      onTap: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) =>  DataPage(key: null,)));

                      },

我得到了错误

The argument type 'Null' can't be assigned to the parameter type 'Key'.

这是我的列表页面代码的一部分

class DataPage extends StatelessWidget {
  const DataPage({
    required Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Hive Test'),
        ),
        body: Column(
          children: <Widget>[
            Expanded(child: _buildListView()),
          ],
        ));
  }

以我目前的知识,我不知道如何解决我的问题。对于任何语法错误,我深表歉意,我会感谢您的帮助!

【问题讨论】:

    标签: flutter dart navigation


    【解决方案1】:

    发生这种情况是因为您在导航到 DataPage 屏幕时在 Key 中传递了“null”。键是可选参数,因此您可以通过这种方式使其为空。

    编辑您的第一个 sn-p:

    Container(
                      height: 150.0,
                      margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
                      child: Material(
                        borderRadius: BorderRadius.circular(20.0),
                        shadowColor: Colors.greenAccent,
                        color: Colors.green,
                        elevation: 7.0,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) =>  DataPage()));
    
                          },
    

    对您的第二个 sn-p 进行编辑:

    class DataPage extends StatelessWidget {
      const DataPage({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('Hive Test'),
            ),
            body: Column(
              children: <Widget>[
                Expanded(child: _buildListView()),
              ],
            ));
      }
    

    【讨论】:

    • 非常感谢!知道它有效
    【解决方案2】:

    您在数据页面中的构造函数如下:

       const DataPage({Key? key}) : super(key: key);
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多