Dictionary 如何转换地图';在flatter中将关键帧从int设置为String

Dictionary 如何转换地图';在flatter中将关键帧从int设置为String,dictionary,flutter,google-cloud-firestore,Dictionary,Flutter,Google Cloud Firestore,我有一个对象列表(这是用户如何填写列表的示例): 这样我就得到了这样的结果: {Press Banca: {0: {repeticiones: 8, peso: 80}, 1: {repeticiones: 6, peso: 90}}} 这正是我想上传到firestore的内容 但我在尝试将其设置为setData()时遇到此错误: 我猜问题出在asMap()上,因为它提供int键。然后,如何将map的键从int转换为String,以便将数据传递到firestore?是的,.asMap()函数返

我有一个对象列表(这是用户如何填写列表的示例):

这样我就得到了这样的结果:

{Press Banca: {0: {repeticiones: 8, peso: 80}, 1: {repeticiones: 6, peso: 90}}}
这正是我想上传到firestore的内容

但我在尝试将其设置为
setData()
时遇到此错误:

我猜问题出在asMap()上,因为它提供int键。然后,如何将map的键从int转换为String,以便将数据传递到firestore?

是的,
.asMap()
函数返回
map
,这就是代码崩溃的地方

您可以在从
.asMap()
函数获取映射后立即添加一个
.forEach()
,该函数可以转换键并将其存储为
映射

请查看此代码段:

  List<String> items = ["one", "two", "three"];
  Map<int, String> basicMap = items.asMap(); //keys as int
  Map<String, String> newMap = new Map<String, String>(); //keys as String

  basicMap.forEach((key, value) {
    newMap.putIfAbsent(key.toString(), () => value);
  });
{Press Banca: {0: {repeticiones: 8, peso: 80}, 1: {repeticiones: 6, peso: 90}}}
E/flutter (30304): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'int' is not a subtype of type 'String'
  List<String> items = ["one", "two", "three"];
  Map<int, String> basicMap = items.asMap(); //keys as int
  Map<String, String> newMap = new Map<String, String>(); //keys as String

  basicMap.forEach((key, value) {
    newMap.putIfAbsent(key.toString(), () => value);
  });
  newMap.forEach((key, value){
    print(key is String);
  }); //prints true for all keys