Home > Enterprise >  Flutter : I wanna understand
Flutter : I wanna understand

Time:03-16

I made researches dut I dod not found anything revealant.

Why dies it works ?

child: Column(
              children: 
              [
                for (dynamic answer in answersToQuestion) 
                  Row(
                    children: [
                      Checkbox(
                        activeColor: Style.primaryColor,
                        onChanged: (value) {
                          setState(() {
                            answerControllers[question["id"]]
                                [answer["libelle"]] = value;
                          });
                        },
                        value: answerControllers[question["id"]]
                            [answer["libelle"]],
                      ),
                      Expanded(
                          child: FlatButton(
                              padding: EdgeInsets.all(0),
                              onPressed: () {
                                setState(() {
                                  answerControllers[question["id"]]
                                          [answer["libelle"]] =
                                      !answerControllers[question["id"]]
                                          [answer["libelle"]];
                                });
                              },
                              child: Align(
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  answer["libelle"],
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ))) 
                    ],
                  ),
              ],
            ));

But, this, it does not work at all ?

child: Column(
              children: 
              [
                for (dynamic answer in answersToQuestion) {
                  Row(
                    children: [
                      Checkbox(
                        activeColor: Style.primaryColor,
                        onChanged: (value) {
                          setState(() {
                            answerControllers[question["id"]]
                                [answer["libelle"]] = value;
                          });
                        },
                        value: answerControllers[question["id"]]
                            [answer["libelle"]],
                      ),
                      Expanded(
                          child: FlatButton(
                              padding: EdgeInsets.all(0),
                              onPressed: () {
                                setState(() {
                                  answerControllers[question["id"]]
                                          [answer["libelle"]] =
                                      !answerControllers[question["id"]]
                                          [answer["libelle"]];
                                });
                              },
                              child: Align(
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  answer["libelle"],
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ))) 
                    ],
                  ),
}
              ],
            ));

Whereas, I added square brackets to the foreach, and it does not sense change anything, but the second code does not work, where is the logical ?

I beliveed that foreach () { justOne } and foreach () justOne was the same ? not ? why ?

CodePudding user response:

The difference is that a code block can be made of a single line or multiple lines: in the latter, you have to surround the code lines with curly braces so the compiler knows that the whole code inside the braces must be treated as a single block. Take this example:

if (something) {
   print(stuff);
   save();
   setState((){});
}

If something is true, the whole code in the braces is executed because of the braces

if (something) 
   print(stuff);
   save();
   setState((){});

In this example, if something is true only the print will be executed, because (usually) the if condition executes only the first code block that follows, and in this case the block is made only of print, while the other two lines get executed regardless of something value. The code formatting and indentation helps a lot in understanding this: in fact, the second example is often formatted in another way, just to be more understandable:

if (something) print(stuff);
save();
setState((){});

In this specific case, you're in the middle of a widget declaration, and this means that code blocks must always be made of single code lines, so your second piece of code throws error.

CodePudding user response:

They are not the same, actually the for (dynamic variable in myList) syntax is kinda new, that helped us to improve an old fashion way of mapping list elements in new classes instances.

To give you an example

Imagine you have a class Test

class Test {
  String name;
  
  Test({ required this.name });
  
  @override
  String toString() {
    return this.name;
  }
}

And you have a list of names:

const names = ['hey', 'test', 'op'];

Before Dart 2.3 the only way that you could map your List into class instances (Or Widgets in Flutter) was the following:

final list1 = names.map((name) => Test(name: name)).toList();  print(list1); // [hey, test, op]

Now you can do this (Your current approach):

final list2 = [for (String name in names) Test(name: name)];
print(list2) // [hey, test, op]

And if you add brackets, as it's a pretty special syntax, Dart can take it in non-expected way

final list3 = [for (String name in names) {
  Test(name: name)
}];

print(list3); // [{hey}, {test}, {op}]

And, actually, the type of list3 is List<Set<String>>, that is not a List<String>, and Flutter's properties usually work under the type of List<Widget>.

Hope that this helps a bit to understand this behavior, but I invite you to play around a bit in Dart Pad:)

  • Related