Where is the error in the else position in Flutter?

57 views Asked by At

enter image description here

At first, I was told that PrettyQrView was wrong, so I changed PrettyQrView(qrCode: qrData) to PrettyQrView.data(data: qrData), and this error disappeared, but the error in else is still the same. I don't know if it's a location issue, but even if I move the location, the error remains the same.

I wanted to fix the error in else and create a code that generates a QR code containing the ID used when the user logs in.

4

There are 4 answers

1
Dhafin Rayhan On

Based on the control flow collection proposal, you can use if-else inside a collection with a syntax like this:

if (cond) expr1 else expr2

Notice how there's no comma (,) before the else keyword.

So in order to make your code works, modify it to follow the syntax:

if (qrData.isNotEmpty)
  PrettyQrView.data(data: qrData)
else
  Text('There is ...')

Alternatively, you can use the conditional operator (known as ternary operator in other languages):

qrData.isNotEmpty
    ? PrettyQrView.data(data: qrData)
    : Text('There is ...')
0
Mäddin On

You have to remove the comma, so not

if(qrData.isNotEmpty) ..., else

but

if(qrData.isNotEmpty) ... else
0
Naser Ebedo On

You can use somthing like this

if ( condition_is_true )
    your_widget_here
else ... [
    your_widget_here
],

or if it's list of widgets you can do like this

if ( condition_is_true ) ... [
    your_widget_here 
    your_widget_here
    your_widget_here
]
else ... [
    your_widget_here
    your_widget_here
    your_widget_here
],
0
Hardik Mehta On

You can do something like this.

if(condition)...[] else <conditional widget>