【问题标题】:Can't I write braces in an if statement in List?Can\'t I write braces in an if statement in List?
【发布时间】:2022-12-26 13:46:05
【问题描述】:

The following without braces worked fine:

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

I prefer to add braces even if there is only one expression.
I did the following and it resulted in an error.

The code that causes an error:

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

Error:

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 {

Can't I write braces in an if statement in List?

Referred to the following:
How to use conditional statement within child attribute of a Flutter Widget (Center Widget)

【问题讨论】:

标签: flutter dart


【解决方案1】:

Do it like this with ternary operator

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

【讨论】:

    【解决方案2】:

    Using If statements inside the UI page is Not recommended, You Can Do What you want in tow way: 1- Using Visibility Widget, to Hide One Widget and show another one.

        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- You Can Use Ternary if statement Like this:

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

    And as I say, using a lot of code inside UI page is note recommended. Don't be shy to ask me any thing if any.

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2022-12-27
      • 2022-12-27
      • 2022-12-01
      • 2022-08-16
      • 2022-12-27
      • 2022-12-01
      • 2021-04-18
      • 2011-10-05
      相关资源
      最近更新 更多