0

Starting with this string...

"color": "0xFF536DFE",

convert this string into a Color constant that could be used in a widget Color parameter?

something like this...

int hexValue = 0xFF536DFE;

I am doing it with some code, but I would image there to be a simpler way of doing this.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
IrishGringo
  • 3,864
  • 7
  • 37
  • 49

1 Answers1

2

You can't convert a string to a const hex value.

Dart limits what evaluations can be done at const creation time.

Not being able to use const sometimes isn't that much of a limitation. It might sum up if it affects a lot of values, but in general it's not a problem.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • does `const` only play role in runtime stuff? Congrats for 300k, love to see that. – CopsOnRoad Oct 29 '18 at 03:08
  • 3
    Thanks! :D Not exactly sure waht you mean with your question. https://stackoverflow.com/questions/21744677/how-does-the-const-constructor-actually-work Const values are canonicalized and multiple instances created with the same constructor parameters result in only a single instance at runtime. This reduces memory usage and therefore less garbage collection. – Günter Zöchbauer Oct 29 '18 at 07:06
  • 1
    If I got you right does it mean that if a `Padding` is created using const 100 times in code, that means it will only have single instance at runtime, however if `const` isn't used while creating `Padding` then we have 100 instances running and taking up memory? – CopsOnRoad Oct 29 '18 at 10:13
  • Right, if you pass the same parameters to `Padding(...)` constructor every time. – Günter Zöchbauer Oct 29 '18 at 10:29
  • 1
    You give crystal clear explanation. Upvoting comments won't increase your reputation, so upvoting your this answer. One more clarification, so if we create padding (both with const) one with value 8.0 and one with 10.0, so they will have 2 instances on the memory. Right? – CopsOnRoad Oct 29 '18 at 10:41
  • 3
    Exactly. Canonicalization only considers const instances equal that have the same value. Const values are immutable, so anything else wouldn't make sense. – Günter Zöchbauer Oct 29 '18 at 10:46