如何获取映射中键内的值的索引

时间:2020-07-01 20:47:35

标签: flutter dart

我有一个简单的映射,每个索引中有2个键,对于第二个键,我有多个值,我的问题是如何分别访问键中的每个值。

List<Map<String, Object>> cards = [
{
  "intro": "6-WEEK BICEPS TRAINING PROGRAM",
  "desc":
      "You’ll train your biceps twice a week, blasting them on Monday with a workout that targets them as your primary muscle group."
},
{
  "intro": "HERE’S YOUR WEEKLY TRAINING SPLIT:",
  "desc": ['Monday: Biceps, triceps, and abs', 'Tuesday: Legs'],

},

];

例如,卡片[1] ['desc']。现在我要访问“星期二:腿”

PS:当我尝试纸牌[1] ['desc'] [2]时,出现此错误 未为类型“对象”定义运算符“ []”。尝试定义运算符'[]'。

1 个答案:

答案 0 :(得分:0)

[1]附加到现有语句的末尾以产生cards[1]['desc'][1]

cards[1]['desc']返回包含两个元素的List'Monday: Biceps, triceps, and abs''Tuesday: Legs'。使用[index]语法,您可以根据其索引来访问List的成员。

此外,List<Map<String, Object>>不是您要存储的正确类型。您的Map包含String个键,迄今所有String个对象。将其更改为List<Map<String, String>>以反映这一点。

如果您需要在Strings中存储多个Map,请执行List<Map<String, dynamic>>,将Object替换为dynamic