3

I know that in the latest version of dart we can use if else statements inside the build method. Does anyone know if we can use also if else statement when we setting class parameters? I know I can do inline statement there but inline is a bit hard to read when there are multiple conditions

const int i = 0;
class Person {
 // NewClass n = NewClass(a: i == 0 ? 'a' : 'b'); //<- inline statement working
    NewClass n = NewClass(a: if(i == 0)  'a' else 'b'); //<- if statement doesn't
}

class NewClass {
  final String a;
  const NewClass({this.a});
}

Edit:

Basically in my case I've got an TextField widget where I set its's type parameter from enum (Type.text, Type.numeric...) According to this parameter I want to set The textField parameters (textCapitalization, maxLength and so on)

15
  • You cant use i here as part of parameter because it is not const, and it cant be const, because it is not static. Maybe you should change logic and init NewClass variable in constructor Commented Apr 15, 2020 at 8:09
  • @KirillMatrosov Sorry for that.. It was just a quick written example. The question remains the same Commented Apr 15, 2020 at 8:12
  • @delmin That if else works only with collection types. If your type is final List<String> a; then you can do something like: NewClass n = NewClass(a: [if(i == 0) 'a' else 'b']); Commented Apr 15, 2020 at 8:33
  • @MidhunMP hmm... that doesn't seems to work in dartpad Commented Apr 15, 2020 at 8:35
  • you can define a method and implement your logic there then use the method return value as result. Commented Apr 15, 2020 at 8:38

3 Answers 3

3

As per your comment, you are already creating an enum for specifying the type of the fields.

enum Type {text, numeric}

Now for specifying the properties of that particular type, you can add an extension on this enum, as shown below:

extension TextFieldProperties on Type {
  int get maxLength {
    if (this == Type.text) {
      return 10;
    }
    return 12;
  }
}

So in your field class you already have a type defined, you can use that type variable to get the properties of that particular type of field.

Type type = Type.text;
print(type.maxLength); // Will print 10
type = Type.numeric;
print(type.maxLength); // Will print 12

Note: It will work only in Dart 2.7 and above

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm... that looks promising... let me try it and I let you know
I got it... yes that works and it makes my widget much readable now. Thank you
3

You want the conditional expression (?:), not the conditional statement or literal entry (if), as you have already discovered.

The reason if doesn't work is that if only works as a statement or as a collection literal entry. It doesn't work in arbitrary expressions. The reason for the distinction is that the if syntax allows you to omit the else branch. That only makes sense in places where "nothing" is a valid alternative. For a statement, "doing nothing" is fine. For a collection, "adding nothing" is also fine.

In an expression context, you must evaluate to a value or throw. There is no reasonable default that we can use instead of "nothing", so an if is not allowed instead of an expression.

1 Comment

Thanks... That is very good explained and it makes sense as well..
0

Doesn't work because this syntax doesn't exist in Dart. The only way to do what you would like to do is to use the ternary operator.

If you try it in the DartPad you will get an error.

I suggest you to use a function to return the right value.

1 Comment

using function is known way how to do this things. For now I'm using inline statement in my case but it makes the code spaghetti therefore I was hoping for if else statement. Also making function for each parameters wouldn't make things any better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.