-1

I'm sending response.body and country name with slash as like germany/ as parameter and converting it to Germany in parser and returning. But if i use parameter, i'm getting error of the getter 'vacTotal' was called on null. If i write "Germany"to countryDataVac["Germany"] the code works correctly. Is it the problem on {"Global": new CountryDataVac()}? The default value is "Global".

main.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

Parser.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 (created by json to 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,
  };
}
  
osunkaya
  • 93
  • 1
  • 1
  • 9
  • Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Nov 14 '21 at 17:55
  • can you provide `CountryDataVac` class so we can understand and help you – evals Nov 14 '21 at 18:06
  • @nvoigt this does not answer. if i write it manually, code works. Maybe it's because of async. or need like setState() – osunkaya Nov 14 '21 at 20:01
  • @evals i added CountryDataVac – osunkaya Nov 14 '21 at 20:01

1 Answers1

0

use

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

instead of

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

i think there is something wrong with your capitalize method

evals
  • 1,750
  • 2
  • 18
  • 28