【问题标题】:ternary operator in return - FLUTTER三元运算符作为回报 - FLUTTER
【发布时间】:2021-11-13 07:47:53
【问题描述】:
  Widget _buildItems() {
    return id == 0
        ? Column(Text("PAGE 1"))
        : Center(
            Text("PAGE 2"),
          );
  }

你好,上面是我的颤振代码的一部分,我一直在出错。你能调查一下吗?看起来我的语法是错误的,但我无法确定我错在哪里。

谢谢。

        ? Column(Text("PAGE 1"))

                ^

packages/flutter/lib/src/widgets/basic.dart:4930:3: Context: Found this candidate, but the arguments don't match.

  Column({

  ^^^^^^

/lib/components/challenge_menu.dart:32:17: Error: Too many positional arguments: 0 allowed, but 1 found.

Try removing the extra positional arguments.

        : Center(

                ^

packages/flutter/lib/src/widgets/basic.dart:1984:9: Context: Found this candidate, but the arguments don't match.

  const Center({ Key? key, double? widthFactor, double? heightFactor, Widget? child })

        ^^^^^^

/lib/components/challenge_menu.dart:30:12: Error: The getter 'id' isn't defined for the class '_ChallengeMenuState'.

 - '_ChallengeMenuState' is from 'package:osam2021/components/challenge_menu.dart' ('/lib/components/challenge_menu.dart').

Try correcting the name to the name of an existing getter, or defining a getter or field named 'id'.

    return id == 0

           ^^

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    每一列都有一个称为子项的必需属性。您必须使用它在列中包含任何子小部件。

    【讨论】:

      【解决方案2】:

      在您直接在 Column 中使用 Text Widget 的情况下,每个 Columns 都有一个“儿童”列表,您必须像这样使用:

        Column(
                 children:[
                             Text("your text"),
                               .....
                               ]
      
                )
      

      【讨论】:

        【解决方案3】:

        列类

        在垂直数组中显示其子项的小部件。

        Column(
          children: const <Widget>[
            Text('Deliver features faster'),
            Text('Craft beautiful UIs'),
            Expanded(
              child: FittedBox(
                fit: BoxFit.contain, // otherwise the logo will be tiny
                child: FlutterLogo(),
              ),
            ),
          ],
        )
        

        for more details

        中心类

        一个以自身为中心的小部件。

        Center(
             child: Container()
        ),
        

        你忘了使用 child 和 children 。

        同时使用两个小部件:

        class LoginPage extends StatelessWidget {
          @override
          Widget build(BuildContext context) {
            return SafeArea(
              child: Scaffold(
                body: Column(
                  children: <Widget>[
                    Center(
                      child: Text('Logo'),
                    ),
                  ],
                ),
              ),
            );
          }
        }  
        

        【讨论】:

          【解决方案4】:

          您错过了 Column 中的孩子和 Center 小部件中的孩子。您应该将一个小部件数组分配给 Column 中的子键,并将 Text() 小部件分配给 Center 小部件中的子键。

          你应该有这样的代码

          Widget _buildItems() {
            return id == 0
              ? Column(children: [Text("PAGE 1")])
              : Center(child: Text("PAGE 2"));
          }
          

          【讨论】:

            【解决方案5】:
            Widget _buildItems() {
            return( id == 0)
                ? Column(children : [Text("PAGE 1")])
                : Center(child : 
                    Text("PAGE 2")
                  );
              }
            

            我不知道你打算用它做什么,但只是修复错误

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-04-02
              • 2016-08-09
              • 2015-12-13
              • 1970-01-01
              • 2013-01-12
              • 1970-01-01
              • 1970-01-01
              • 2021-04-28
              相关资源
              最近更新 更多