Poperton
Poperton

Reputation: 2226

Ternary operator in Flutter to return List<Widget>

I'm trying to return a list of 2 widgets depending on the swapped value. I tried:

Container(child: Row(children: swapped? [a, b]: [b, a],),

where a and b are Widget:

Widget a = ...
Widget b = ...

But I'm getting

The operator '[]' isn't defined for the type 'bool'.

What am I doing wrong?

swapped is bool

I also tried

children: swapped? List<Widget>([a, b]): List<Widget>([b, a])

and got

The argument type 'List<Widget>' can't be assigned to the parameter type 'int'.

Upvotes: 1

Views: 1980

Answers (3)

jamesdlin
jamesdlin

Reputation: 90125

The operator '[]' isn't defined for the type 'bool'.

I can't seem to reproduce your problem with DartPad, but it sounds like you're running into an ambiguity between the ternary operator and the new null-aware subscript operator that was introduced with Dart's new null-safety feature.

I also tried

children: swapped? List<Widget>([a, b]): List<Widget>([b, a])

and got

The argument type 'List<Widget>' can't be assigned to the parameter type 'int'.

You get that error because List<Widget>([a, b]) does not do what you think it does. Dart doesn't support overloaded functions, so you're invoking the (now deprecated) List constructor that takes a single length argument and are attempting to pass a List as the length.

What you actually want, which I think should resolve the original ambiguity, is:

children: swapped ? <Widget>[a, b]: <Widget>[b, a],

(Since I can't reproduce your problem, it'd be useful to know what version of Dart you're using and if you could provide a minimally reproducible example.)

Upvotes: 0

Prateek
Prateek

Reputation: 113

I have also faced similar problems when using the ternary operator in Colors which results in similar error, so what's the solution?

Wrap your condition of ternary in parenthesis like below

Container(child: Row(children: (swapped? [a, b]: [b, a]),),)

What was the real pronlem: As your were writing condition to check that is swapped is true or not the dart was just going through boolean first but as everyone and even dart know that book!= List then it rejects your code and hence results in an error.

Conclusion :

Use parenthesis with ternary operators whenever possible.

Upvotes: 2

Azad Prajapat
Azad Prajapat

Reputation: 806

checkout the following code on dartpad its working

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
 bool swapped =true;
    Widget a=Text("2");
    Widget b = Text("4");

    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Container(
                  child: Row(
                    children: swapped? [a,b]: [b,a],
                  ), 
      ),
        )
    );
  }
}

 

Upvotes: 0

Related Questions