Search code examples
jsonflutternestedfluttermap

How can I define a Map using another Map in Flutter?


I have a Map like following:

  final Map<String, dynamic> question = {
    'rating': {
      'added': false,
    }
  };

And I want to use it inside another Map like below:

  final Map<String, dynamic> book = {
    'detail': {
      'questions': [
        question // here I need to have a list of the above Map
      ]
    }
  };

But it doesn't work. How can I use another Map's definition inside a Map I am gonna define as it's parent?


Solution

  • try to put the spread operator "..." like this:

       final Map<String, dynamic> book = {
    'detail': {
      'questions': [...{question}] // here I need to have a list of the above Map
      
    }};
    

    [EDIT] try this instead,it should work fine now :

     late final Map<String, dynamic> book = {
    'detail': {
      'questions': [...{question}] // here I need to have a list of the above Map   
    }};