Federick Jonathan
Federick Jonathan

Reputation: 2864

How to get rid of 'Avoid using unnecessary statements' while using multiple statements in Ternary Operator

This is what I wanna do: check if the list contains 'something', if true => remove it => add 'anything' => print the list, else => add 'something' => print the list

List<String> components = List<String>();

ListTile(
 title: Text('Title'),
 onTap: () {
  components.contains('something')
   ? () {
      components.removeWhere((item) => item == 'something');
      components.add('anything');
     }
   : components.add('something');
  print(components);
 }
)

It runs just fine and the onTap function is working properly as it is supposed to but I get a blue curly underline around here:

? () {

It says 'Avoid using unnecessary statements.dart(unnecessary_statements)'. Is there a better approach on what I want to achieve?

Upvotes: 1

Views: 6092

Answers (3)

Crazy Lazy Cat
Crazy Lazy Cat

Reputation: 15053

Normal function definition will look like this

myFunction(){
  ...
  ...
}

This is just a kind of declarations only.

To execute them you have to call it like myFunction() from somwhere.

Likewise definition of anonymous function will be like.

() { //without name
  ..
  ..
}

So to execute you need to add () at the end.

You only gave function definition after ?. you have to call that function by adding () at the end

ListTile(
  title: Text('Title'),
  onTap: () {
    components.contains('something')
        ? () {
            components.removeWhere((item) => item == 'something');
            components.add('anything');
          }() //TODO: Make it as function call
        : components.add('something');
    print(components);
  },
)

Upvotes: 2

Anil Chauhan
Anil Chauhan

Reputation: 707

Try like this, (There can be lots of way to this more efficiently)


  List<String> components = List<String>();

  void fun1(int index, dynamic otherData) {
      components.removeWhere((item) => item == 'something');
      components.add('anything');
      print('sflbknsfb');
  }

  void fun2 () {
      print('ssvskfjfsflbknsfb');

    components.add('something');
  }

ListTile(
    title: Text('Title'),

    // onTap: () {
    //   components.contains('something') ? fun1() : fun2();
    //   },

    //Or 

   onTap:() => components.contains('something') ? fun1() : fun2(),
 ),

Upvotes: 1

Pablo Barrera
Pablo Barrera

Reputation: 10963

To keep your code readable and maintainable it would be better to do something like this:

ListTile(
    title: Text('Title'),
    onTap: _onTapTitle,
);

void _onTapTitle() {
  if (components.contains('something')) {
    components.removeWhere((item) => item == 'something');
    components.add('anything');
  } else {
    components.add('something');
  }
  print(components);
}

If you need to pass some data to the _onTapTitle() you could do this:

onTap: () => _onTapTitle(data),

Also, it might be better to move the _onTapTitle() logic to the bloc if you use BLoC pattern.

Upvotes: 2

Related Questions