Search code examples
dartstring-interpolation

Dart string interpolation - invalid constant value


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


Solution

  • 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("...