Aditya
Aditya

Reputation: 425

Evaluation of this constant expression throws an exception. error in dart

this is my code:

const InputDecoration kTextFieldDecoration = InputDecoration(
    filled: true,
    fillColor: Colors.white,
    icon: Icon(Icons.location_city, color: Colors.white,),
    hintText: "Enter city name",
    hintStyle: TextStyle(
        color: Colors.grey
    ),
    border: OutlineInputBorder(
      borderSide: BorderSide.none,
      borderRadius: BorderRadius.circular(10)
    )
);

I am getting the error whenever I add const.

In this question it is given that we have to give compile time constants to declare it as const, but in my code I have given compile time constants.

I saw somewhere that if constructor was const then I can't use const while creating object, what is the reason for this?

The error comes only for OutlineInputBorder as it has const constructor.

Upvotes: 0

Views: 2508

Answers (1)

Peter Koltai
Peter Koltai

Reputation: 9734

If you check the documentation here, you will see that BorderRadius.circular(radius) internally is the same as BorderRadius.all(Radius.circular(radius)).

Try to replace this line in you code:

borderRadius: BorderRadius.circular(10)

with this:

borderRadius: BorderRadius.all(Radius.circular(10))

Now it will work with const. The explanation is that BorderRadius.all(Radius.circular(10)) is also a constant, therefore valid inside another constant declaration. But the shorthand version you use can only be evaluated during runtime because of the this keyword in BorderRadius.circular constructor.

Upvotes: 3

Related Questions