简体   繁体   English

谁能解释 function of.map()

[英]Can anyone explain the function of .map()

I was trying to make a Covid Tracking application using flutter, and I came across this function getCountrySummary( ) ,我试图使用 flutter 制作 Covid Tracking 应用程序,我遇到了这个 function getCountrySummary()

import 'package:covid_tracker/models/country_summary.dart';
import 'package:covid_tracker/models/global_summary.dart';

import 'package:http/http.dart' as http;
import 'dart:convert';

class CovidService {
  Future<GlobalSummaryModel> getGlobalSummary() async {
    final data =
        await http.get(Uri.parse('https://api.covid19api.com/summary'));

    if (data.statusCode != 200) {
      throw Exception();
    }

    GlobalSummaryModel summary =
        GlobalSummaryModel.fromJson(json.decode(data.body));

    return summary;
  }

  Future<List<CountrySummaryModel>> getCountrySummary(String slug) async {
    String url = "https://api.covid19api.com/total/dayone/country/$slug";

    final data = await http.get(Uri.parse(url));

    if (data.statusCode != 200) {
      throw Exception();
    }

    List<CountrySummaryModel> summaryList = (json.decode(data.body) as List)
        .map((item) => CountrySummaryModel.fromJson(item))
        .toList();

    return summaryList;
  }
}

So I know what the function getCountrySummary() is trying to do, but I don't understand what statement所以我知道 function getCountrySummary() 试图做什么,但我不明白什么说法

List<CountrySummaryModel> summaryList = (json.decode(data.body) as List).map((item) => CountrySummaryModel.fromJson(item)).toList();

is trying to do, and CountrySummaryModel is an object.正在尝试做,而 CountrySummaryModel 是 object。

class CountrySummaryModel {
  final String country;
  final int confirmed;
  final int death;
  final int recovered;
  final int active;
  final DateTime date;

  CountrySummaryModel(this.country, this.active, this.confirmed, this.date,
      this.death, this.recovered);

  factory CountrySummaryModel.fromJson(Map<String, dynamic> json) {
    return CountrySummaryModel(
      json["country"],
      json["active"],
      json["confirmed"],
      DateTime.parse(json["date"]),
      json["death"],
      json["recovered"],
    );
  }
}

When you call Map on a list, it means you want to reach each item in it, in your case you call map on your list to parse each item in it and at then call toList() to make a list of this items.当您在列表上调用Map时,这意味着您想要访问其中的每个项目,在您的情况下,您在列表中调用 map 以解析其中的每个项目,然后调用toList()来制作这些项目的列表。

If I understand your code correctly:如果我正确理解您的代码:

  1. First, you convert data to List .首先,将data转换为List

  2. Then, use CountrySummaryModel.fromJson() and .toList() to convert it to List<CountrySummaryModel> .然后,使用 CountrySummaryModel.fromJson( CountrySummaryModel.fromJson().toList()将其转换为List<CountrySummaryModel>

暂无
暂无

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

相关问题 谁能解释以下Javascript代码行为? - Can anyone explain the following Javascript code behaviour? 谁能解释这个删除评论的正则表达式的工作原理? - Can anyone explain how this comment-removing regex works? 谁能解释为什么我的程序没有显示正确的警报? - Can anyone explain why my program is not showing the correct alert? 谁能解释一下这张JSON数值的图表 - Can anyone please explain this chart of JSON number values 任何人都可以解释如何将JSON Schema链接到我的JSON以验证JSON - Can anyone explain how can i link the JSON Schema to my JSON to validate the JSON against 谁能解释如何在Activity外部初始化领域实例? 例如,当用截击和gson解析json时? - can anyone explain how to initialize realm instance outside the Activity? for example while parsing json with volley and gson? 谁能解释如何在空手道中获得两个响应之间的所有不匹配? - Can anyone explain how to get all the mismatch between two responses in karate? “ JSON不支持注释,因为开发人员曾经将指令传递给解析器”。 谁能解释这意味着什么? - “Comments are not Supported in JSON because developers used to pass instructions to the parser”. Can anyone explain what this means? 任何人都可以使用json_encode和json_decode解释这个PHP代码吗? - Can anyone explain this PHP code using json_encode and json_decode? GEOJSON 为什么给出 3 个坐标系,谁能更简单地解释一下它的属性 - GEOJSON Why 3 coordinate system is given, can anyone explain its attribute more briefly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM