flutter 我不能在List的if语句中写大括号吗?

bsxbgnwa  于 2022-12-27  发布在  Flutter
关注(0)|答案(4)|浏览(237)
    • 以下不带大括号的代码运行良好:**
import 'package:flutter/material.dart';

void main() {
  runApp(const _MyApp());
}

class _MyApp extends StatelessWidget {
  const _MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    const isFlag = true;

    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: const [
            Text(
              "Demo1",
            ),
            if (isFlag)
              Text(
                "Demo true",
              )
            else
              Text(
                "Demo flase",
              )
          ],
        ),
      ),
    );
  }
}

即使只有一个表达式,我也更喜欢添加大括号。
我执行了以下操作,结果出现错误。

    • 导致错误的代码:**
import 'package:flutter/material.dart';

void main() {
  runApp(const _MyApp());
}

class _MyApp extends StatelessWidget {
  const _MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    const isFlag = true;

    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: const [
            Text(
              "Demo1",
            ),
            if (isFlag) {
              Text(
                "Demo true",
              )
            } else {
              Text(
                "Demo flase",
              )
            }
          ],
        ),
      ),
    );
  }
}
    • 错误:**
lib/main.dart:21:25: Error: A value of type 'Set<Text>' can't be assigned to a variable of type 'Widget'.
 - 'Set' is from 'dart:core'.
 - 'Text' is from 'package:flutter/src/widgets/text.dart'
 ('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/text.dart').
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart'
 ('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/framework.dart').
            if (isFlag) {
                        ^
lib/main.dart:25:20: Error: A value of type 'Set<Text>' can't be assigned to a variable of type 'Widget'.
 - 'Set' is from 'dart:core'.
 - 'Text' is from 'package:flutter/src/widgets/text.dart'
 ('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/text.dart').
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart'
 ('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/framework.dart').
            } else {

我不能在List的if语句中写大括号吗?
参考以下内容:
How to use conditional statement within child attribute of a Flutter Widget (Center Widget)
附言:
问题只在于是否可以应用大括号。
我问这个问题是因为我对Dart语法感兴趣。

6yoyoihd

6yoyoihd1#

你正在创建一个使用大括号的集合来阅读更多关于SET访问集合常量的信息。你的构建函数应该是这样的,

return MaterialApp(
   home: Scaffold(
     body: Column(
       children: const [
         Text(
           "Demo1",
         ),
         if (isFlag)
           Text(
             "Demo true",
           )
         else
           Text(
             "Demo flase",
           )
       ],
     ),
   ),
);

也可以使用ternary运算符,示例代码

return MaterialApp(
   home: Scaffold(
     body: Column(
       children: const [
         Text(
           "Demo1",
         ),
         isFlag ? Text("Demo true",) : Text("Demo flase")
       ],
     ),
   ),
);
nr7wwzry

nr7wwzry2#

用三元运算符这样做

isFlag?Text("Demo true"):Text("Demo flase")
8ulbf1ek

8ulbf1ek3#

不建议在UI页中使用If语句,您可以通过以下两种方式执行您想要的操作:1-使用可见性小部件,隐藏一个小部件并显示另一个。

return MaterialApp(
  home: Scaffold(
    body: Column(
      children: const [
        Text(
          "Demo1",
        ),
        Visibility(
         visible : isFlag, 
         child: Text(
            "Demo true",
          ),
         )
         Visibility(
         visible : !(isFlag), 
         child: Text(
            "Demo false",
          ),
         )
      ],
    ),
  ),
);

2-你可以像这样使用三元if语句:

return MaterialApp(
  home: Scaffold(
    body: Column(
      children: const [
        Text(
          "Demo1",
        ),
        isFlag ? Text("Demo true",) : Text("Demo false"), 
      ],
    ),
  ),
);

就像我说的,在UI页面中使用大量的代码是不推荐的。如果有的话,不要羞于问我任何事情。

fsi0uk1n

fsi0uk1n4#

[ 
     if(kDebugMode){
         Text('Data')
        }.first,
   ]

{}正在将其设置为集合,以便您可以像在{}中一样访问其数据。first从集合中获取第一个对象/值,在您的情况下,它被视为具有未定义数量对象的集合。

[
     if (!kDebugMode) ...{
        Text('Data'),
        Text('data'),
        },
     ]

或者,还可以对需要相同if条件的小部件使用扩展运算符(...)

相关问题