繁体   English   中英

Flutter Dart getter '' 在 null 上被调用

[英]Flutter Dart The getter '' was called on null

我正在发送 response.body 和带有斜杠的国家germany/地区名称,如germany/作为参数,并在解析器中将其转换为Germany并返回。 但是如果我使用参数,我会收到the getter 'vacTotal' was called on null 如果我将"Germany"写入 countryDataVac["Germany"] 代码可以正常工作。 是 {"Global": new CountryDataVac()} 上的问题吗? 默认值为“全局”。

主要.dart:

  Map<String, CountryDataVac> countryDataVac = {"Global": new CountryDataVac()};
  static Map<String, CountryDataVac> getCountryDataVac(String body, var countryname) {
    Map<String, CountryDataVac> countryDataVac = {};

responseVac1Day = await http.get("https://disease.sh/v3/covid-19/vaccine/coverage?fullData=true&lastdays=1"); //vaccine number info
      countryDataVac = Parser.getCountryDataVac(responseVac1Day.body, "germany/"); //vaccine number info

解析器.dart:

var usera = CountryDataVac.fromJson(jsonDecode(result));

countryDataVac[capitalize(countryname).replaceAll("/", "")] = parseRowVac(usera.vacTotal,usera.vacDaily,usera.vactotalPerHundred,usera.vacdailyPerMillion); //gives 'germany/' as 'Germany' but getting error if i get the countryname by parameter

countryDataVac["Germany"] = parseRowVac(usera.vacTotal,usera.vacDaily,usera.vactotalPerHundred,usera.vacdailyPerMillion); //works

}

CountryDataVac.dart(由 json 创建到 dart)

import 'dart:convert';

List<CountryDataVac> countryDataVacFromJson(String str) => List<CountryDataVac>.from(json.decode(str).map((x) => CountryDataVac.fromJson(x)));

String countryDataVacToJson(List<CountryDataVac> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class CountryDataVac {
  CountryDataVac({
    this.vacTotal = 0,
    this.vacDaily = 0,
    this.vactotalPerHundred = 0,
    this.vacdailyPerMillion = 0,
    this.date = "",
  });

  int vacTotal = 0;
  int vacDaily = 0;
  int vactotalPerHundred = 0;
  int vacdailyPerMillion = 0;
  String date = "";

  factory CountryDataVac.fromJson(Map<String, dynamic> json) => CountryDataVac(
    vacTotal: json["total"],
    vacDaily: json["daily"],
    vactotalPerHundred: json["totalPerHundred"],
    vacdailyPerMillion: json["dailyPerMillion"],
    date: json["date"],
  );

  Map<String, dynamic> toJson() => {
    "total": vacTotal,
    "daily": vacDaily,
    "totalPerHundred": vactotalPerHundred,
    "dailyPerMillion": vacdailyPerMillion,
    "date": date,
  };
}
  

countryname.replaceFirst(countryname[0], countryname[0].toUpperCase()).replaceAll("/", "");

代替

capitalize(countryname).replaceAll("/", "")

我认为你的大写方法有问题

暂无
暂无

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

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