Home > database >  I wonder how to solve the 'List' error of 'flutter'
I wonder how to solve the 'List' error of 'flutter'

Time:07-30

   var questions = [
     {
       'questionText : what is your favorite color?'
       ,
       'answer': [
         {'Black'}, {'Red'}, {'Green'}, {'Blue'}, {'White'},
       ],       
     }];

Error : The literal can't be either a map or a set because it contains at least one literal map entry or a spread operator spreading a 'Map', and at least one element which is neither of these.

flutter ver 3

Mac M1

CodePudding user response:

I suppose you actually want this?

   var questions = [
     {
       'questionText' : 'what is your favorite color?'
       ,
       'answer': [
         {'Black'}, {'Red'}, {'Green'}, {'Blue'}, {'White'},
       ],       
     }];

Now let's talk about the error:

Error : The literal can't be either a map or a set
because it contains at least one literal
map entry
or a
spread operator spreading a 'Map'
and at least
one element which is neither of these.

I suppose you are clear what are dart sets because you used it in your code. So I'll start with map entry.

A map entry is something like this

'key': 'value'

This is the usual way of creating Map indices.

spread operator spreading a 'Map' means this:

Map<String, String> a = {'a':'b'};
Map<String, String> b = {'c':'d', ...a}; // b = {'c':'d', 'a':'b'}

The spread operator is three dots followed by an Iterable. It extends the variable by that Iterable.

You used a map entry 'answer': [{'Black'}, {'Red'}, {'Green'}, {'Blue'}, {'White'},], and something else: 'questionText : what is your favorite color?'

I personally believe this is a typo but I still want to make it clear.

  • Related