【问题标题】:Flutter error: Column's children must not contain any null values, but a null value was found at index 0Flutter 错误:列的子项不得包含任何空值,但在索引 0 处找到空值
【发布时间】:2020-12-08 14:53:08
【问题描述】:

我在 android studio 中创建了一个应用程序来从一个屏幕导航到另一个屏幕。这里将两个无状态小部件创建为两个屏幕,并且都包含一个按钮来相互导航页面。 但是,当我运行该应用程序时,我的 android 手机上会生成一个红屏,我收到一条错误消息

exception 'Column's children must not contain any null values, but a null value was found at index 0'.

我在下面提供了我的代码:

首屏

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("First Screen"),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              center(
                decoration: new BoxDecoration(
                  image: new DecorationImage(
                    image: new AssetImage('assets/new 7wonders.jpg'),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              Text('New 7 Wonders',
                style: TextStyle(fontSize: 40, fontStyle: FontStyle.italic),
              ),
              RaisedButton(
                child: Text("Bang Here"),
                onPressed: (){
                  Navigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen()));
                },
                color: Colors.red,
                textColor: Colors.yellow,
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                splashColor: Colors.grey,
              )
            ],
          ),
        ),
      ),
    );
  }

  center({BoxDecoration decoration}) {}
}

第二屏

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: RaisedButton(
        child: Text("Go to First page"),
        onPressed:() {
          Navigator.pop(context);
        },
      ),
    );
  }
}

【问题讨论】:

  • 如果我的回答对您原帖中指定的问题有帮助,请采纳。如果没有,请提供更多详细信息说明为什么没有。
  • 它帮助了我。解决方案就在现场。

标签: android-studio flutter flutter-layout


【解决方案1】:

您的center 方法应该返回一个小部件,它目前正在向Column 提供null

改为这样做:

 Widget center() {
    // return a decorated box widget which gives you the decoration property
    return Image(
          image: AssetImage(
          'assets/new 7wonders.jpg',),
           fit: BoxFit.cover,
     );
  }
}

然后在你的Column 中使用:

Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
             // call the center method which returns a Widget
              center(),
              Text(
                'New 7 Wonders',
                style: TextStyle(fontSize: 40, fontStyle: FontStyle.italic),
              ),
              RaisedButton(
                child: Text("Bang Here"),
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => SecondScreen()));
                },
                color: Colors.red,
                textColor: Colors.yellow,
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                splashColor: Colors.grey,
              )
            ],
          ),
        ),
      ),

【讨论】:

  • 代码正在运行,但不幸的是这个资产图像没有出现在第一个屏幕中。
  • 不,先生,资产图像仍未出现在第一个屏幕中,我什至更改了资产图像并决定包含网络图像,但图像仍未在第一个屏幕中加载。
【解决方案2】:
Remove center use this

Container(
   height: 100,       // height and width according to your ui
     width:100,
       child:Image.asset(('assets/new7wonders.jpg',fit: BoxFit.cover,), // use for local image from asset and please change image name in code as well as in asset folder.there should  not be space between image name .
      
 ),  

【讨论】:

  • 代码正在运行,但不幸的是这个资产图像没有出现在第一个屏幕中。
  • 我稍后会检查它。我会更新你不用担心这个。
【解决方案3】:

在第 24 行,您返回了空值。您可以像这样实现 center 方法;

return Container();

【讨论】:

    【解决方案4】:

    您尝试在第 24 行写 Center 而不是 center? 并且必须在 Center 中返回,例如 Containter()

    【讨论】:

      【解决方案5】:

      您必须在中心返回任何小部件

      center({BoxDecoration decoration}) {
         return Container();
      }
      

      【讨论】:

      • 代码正在运行,但不幸的是这个资产图像没有出现在第一个屏幕中。
      猜你喜欢
      • 2020-12-02
      • 1970-01-01
      • 2021-06-10
      • 2021-10-29
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      • 2021-07-31
      相关资源
      最近更新 更多