简体   繁体   English

Flutter 错误:列的子项不得包含任何 null 值,但在索引 0 处找到 null 值

[英]Flutter error: Column's children must not contain any null values, but a null value was found at index 0

I created an application in android studio to navigate from one screen to another.Here two stateless widgets are created as two screens and both contain a button to navigate pages each other.我在 android 工作室中创建了一个应用程序,用于从一个屏幕导航到另一个屏幕。这里将两个无状态小部件创建为两个屏幕,并且都包含一个用于相互导航页面的按钮。 However when i run the application a red screen is generated on my android phone I get an error saying但是,当我运行该应用程序时,我的 android 手机上会生成一个红屏,我收到一条错误消息

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

I have provided my code below:我在下面提供了我的代码:

FIRST SCREEN第一个屏幕

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}) {}
}

SECOND SCREEN第二个屏幕

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);
        },
      ),
    );
  }
}

Your center method should return a Widget, it is currently providing null to the Column .您的center方法应该返回一个小部件,它目前正在向Column提供null

Do this instead:改为这样做:

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

Then use in your Column like:然后在您的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,
              )
            ],
          ),
        ),
      ),

you have to return any widget in center您必须在中心返回任何小部件

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

You tryed write Center instead center in line 24?您尝试在第 24 行写Center而不是center And in Center must be will return for example Containter()并且必须在 Center 中返回,例如Containter()

In 24th line, you returned null value.在第 24 行,您返回了 null 值。 You can implement the center method like this;您可以像这样实现中心方法;

return Container();
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 .
      
 ),  

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

相关问题 Flutter null 安全迁移错误 Null 检查在 null 值上使用的运算符 - 发生在 PageTransformer ScrollMetrics object - Flutter null safety migration error Null check operator used on a null value - occured at PageTransformer ScrollMetrics object 毕加索目标不得为空错误 - Picasso Target must not be null error 在iPhone模拟器上运行Flutter应用程序时偶尔会出现IDE错误。 FlutterApp.setLaunchMode不能为null - Occasional IDE error when running Flutter app on iPhone simulator. FlutterApp.setLaunchMode must not be null Build Gradle错误“名称不能为空” - Build Gradle error “name must not be null” kotlin android中的“findViewById不能为空”错误 - "findViewById must not be null" error in kotlin android Android中的IllegalArgumentException错误,Context不能为null - IllegalArgumentException error in Android,Context must not be null 错误:条目中的空值:blameLogFolder = null - Error: null value in entry: blameLogFolder=null AndroidStudio 错误:条目中的空值:workingDir=null - AndroidStudio error: null value in entry: workingDir=null 单击ScrollView后,索引1处的绑定值为null - The bind value at index 1 is null after clicking ScrollView 错误:条目中的空值:streamOutputFolder = null或错误:条目中的空值:streamOutputFolder = null - Error:null value in entry: streamOutputFolder=null OR Error:null value in entry: streamOutputFolder=null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM