简体   繁体   English

有没有办法在 Flutter 中获取地图中单个对象的地图中的键值?

[英]Is there any way to get the key value in a map for a single object in the map in Flutter?

I've searched a lot but could not find a case where one can retrieve the value of the key for a Map object.我已经搜索了很多,但找不到可以检索 Map 对象的键值的情况。 My use case involves retrieving data as a json from Http, decoding it to a map, then getting the data from the map, including the keys, and store it as an object.我的用例涉及从 Http 以 json 形式检索数据,将其解码为映射,然后从映射中获取数据(包括键),并将其存储为对象。 Most cases here talk about retrieving all the keys, which I don't want.这里的大多数情况都涉及检索所有密钥,这是我不想要的。 My json structure is as follows, FWIW:我的json结构如下,FWIW:

DB
|__jobs
    |___key1
          |____title
          |____company
          |____salary

I decided to change my factory in my classes so that I can pass the key as a parameter:我决定在我的课程中更改我的工厂,以便我可以将密钥作为参数传递:

  factory Job.fromJson(String jobID, Map<String,dynamic> json) {
    return Job(
        uid: jobID,
    );
  }

And in my decode function:在我的解码功能中:

    map.forEach((jobsID, jobsData) {
      _jobsList.add(Job.fromJson(jobsID, jobsData));
    });

I haven't tested whether it will cause issues in future though.我还没有测试它是否会在未来引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM