颤抖的孩子内部验证错误

时间:2020-03-15 12:36:22

标签: flutter dart

我试图在自己的孩子中做一个自我,但是如果我在烦躁的孩子中犯错,这就会给我一个错误。 我在做什么错了?

我想做的是:

if(snapshot.data.Facebook != null){
   // display the image if different from null
   Image.asset("assets/images/facebook.png",height: 40,), SizedBox(width: 10),
 }

我的代码:-------------------------------------------- --------------

Padding(
     padding: EdgeInsets.only(top: 50),
     child: Row(
     mainAxisSize: MainAxisSize.min,
     children: [

     if(snapshot.data.Facebook != null){

     };


                              Image.asset("assets/images/facebook.png",height: 40,), SizedBox(width: 10),

                              Image.asset("assets/images/instagram.png", height: 40,), SizedBox(width: 10),

                              Image.asset("assets/images/linkdin.png", height: 40,), SizedBox(width: 10),

                              Image.asset("assets/images/pinterest.png", height: 40,), SizedBox(width: 10),

                              Image.asset("assets/images/skype.png", height: 40,), SizedBox(width: 10),

                              Image.asset("assets/images/twitter.png", height: 40,), SizedBox(width: 10),

                              Image.asset("assets/images/whatsapp.png", height: 40,), SizedBox(width: 10),
                            ],
                          ),
                        )

enter image description here

2 个答案:

答案 0 :(得分:1)

您必须创建一个返回Widget列表的函数,并将该函数添加到您的行中。像

更新:

  List<Widget> populateRow() {
    if(snapshot.data.Facebook != null){
      return [Image.asset("assets/images/facebook.png",height: 40,)], 
    } else if (snapshot.data.Instagram != null){
      return [Image.asset("assets/images/instagram.png",height: 40,)],
    } ...

    return [];
  }

并在您的行中添加如下功能

children: populateRow()

请检查是否有效。我还没有测试。显然,您可以修改该功能以显示所需的任何图像

答案 1 :(得分:0)

这是小部件内部if / else的版本。如果条件为true,将显示?前面的小部件,而:则为错误条件。

Padding(
            padding: EdgeInsets.only(top: 50),
            child: snapshot.data.Facebook != null //check the condition
                //return Row with images in it if snapshot.data.Facebook != null is true
                ? Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Image.asset(
                        "assets/images/facebook.png",
                        height: 40,
                      ),
                      SizedBox(width: 10),
                      Image.asset(
                        "assets/images/instagram.png",
                        height: 40,
                      ),
                      SizedBox(width: 10),
                      Image.asset(
                        "assets/images/linkdin.png",
                        height: 40,
                      ),
                      SizedBox(width: 10),
                      Image.asset(
                        "assets/images/pinterest.png",
                        height: 40,
                      ),
                      SizedBox(width: 10),
                      Image.asset(
                        "assets/images/skype.png",
                        height: 40,
                      ),
                      SizedBox(width: 10),
                      Image.asset(
                        "assets/images/twitter.png",
                        height: 40,),
                      SizedBox(width: 10),
                      Image.asset(
                        "assets/images/whatsapp.png",
                        height: 40,
                      ),
                      SizedBox(width: 10),
                    ],
                  )
                //return empty Row if snapshot.data.Facebook != null is false
                : Row(
                    children: <Widget>[
                       //you can add more widget in here
                    ],
                  )
          )