0

I am trying to follow this dart guide https://codelabs.developers.google.com/codelabs/dart-patterns-records#4 but ran into an issue with string interpolation.

I am totally puzzled why dart expects a constant value to produce an interpolated string, this contradicts the goal of string interpolation : to produce a string at runtime from a value stored in a variable. What's wrong here ? Or is it the Text widget constructor that requieres a constant text ? Yet again, that sounds foolish to me.

enter image description here

Here is the metadata getter for Document class:

class Document 
{
  final Map<String, Object?> _json;
  Document() : _json = jsonDecode(documentJson);

    ({String title, DateTime modified}) get metadata 
    {     
      const title = 'My Document';
      final now = DateTime.now();
      return (title: title, modified: now);
    }  
}

I tried to fix this in several ways, but all failed:

enter image description here

enter image description here

3
  • 2
    "body: const Column("... remove the const there. Commented Jan 26 at 19:25
  • @RandalSchwartz Thank you, I didn't spot the constant there 🤦‍♂️ Commented Jan 27 at 11:14
  • The clue is when you see something "can't be const", search immediately outward to find the nearest const and remove it. It should let it compile then, but perhaps warn you that some other parts of the original expression are still const-able, and you can add const back to those portions. Commented Jan 27 at 17:29

1 Answer 1

3

Since the metadataRecord is passed in it is not a constant, and that means that no widget/value that depends on it can be a constant either.

As Randal commented, change

body: const Column("...

To:

body: Column("...
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, obviously...thx

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.