简体   繁体   English

Flutter 中带有容器小部件的 If 语句

[英]If Statement With a Container Widget in Flutter

I created a condition so that if the value is different from null I want the container with that information to appear, if the value is null I don't want to show anything but the problem is that a null text is appearing instead.我创建了一个条件,如果值与 null 不同,我希望显示包含该信息的容器,如果值为 null 我不想显示任何内容,但问题是出现了 Z37A6259CC0C1DAE29BD9A7866489DFF。 What can be happening?会发生什么?

@override
  Widget build(BuildContext context) {
return Scaffold(
          backgroundColor: Colors.white,
          body: Column(
            children: [
            Container(
                child: ("$_info" != null)
                    ? Container(
                        padding:
                            EdgeInsets.symmetric(horizontal: 20, 
                            vertical: 35),
                        child: Text(
                          "$_info",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              fontFamily: 'Montserrat',
                              fontSize: 14,
                              fontWeight: FontWeight.w500),
                        ),
                      )
                    : Container(),
                 ),
               ],
              ),
             };

You shouldn't create a new String from _info and test if it's different from null as in您不应该从_info创建一个新字符串并测试它是否与 null 不同,如

"$_info" != null

This is always going to be true, because all Strings are different from null.这总是正确的,因为所有字符串都不同于 null。 So you're going to see the text "null" appearing on the screen because you have Text("$_info") later on.因此,您将看到文本“null”出现在屏幕上,因为稍后您有Text("$_info")

Instead, you should just test if _info itself is different from null.相反,您应该只测试_info本身是否与 null 不同。 So:所以:

_info != null

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

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