Flutter-访问列表〈Map〈String,dynamic>>中的属性

dgsult0t  于 2023-01-05  发布在  Flutter
关注(0)|答案(1)|浏览(130)

我有一个名为_users的变量,类型为'List〈Map〈String,dynamic〉〉,它有一个名为email的属性,我正在尝试访问它

我试着像这样访问它,但它不工作

var emails = _users.map((element) => element["email"]).toList();
          for (String? element in emails) {
            TaggedUsersSingleton.instance.taggedUsers.add(element);
          }
        }

这就是我得到的错误

════════ Exception caught by foundation library ════════════════════════════════
The following _TypeError was thrown while dispatching notifications for AnnotationEditingController:
type 'List<dynamic>' is not a subtype of type
9gm1akwq

9gm1akwq1#

得到的Map是一个列表的列表。你可能想把它变平。我相信把第一行改为这样可能会做到这一点:

var emails = _users.expand((element) => element["email"]).toList();

示例:

List<Map<String,dynamic>> _users = [{'email': ['a']},{'email': ['b']},{'email': ['c', 'd']}];
  var emails = _users.expand((element) => element["email"]).toList();
  for (String? element in emails) {
    print(element);
  }

输出:

a
b
c
d

相关问题