Я получаю значения из запроса API и сохраняю их в String
переменных для добавления в List<String>
. При печати этой переменной печатается нормально, но тогда я получаю NoSuchMethodError: The method 'add' was called on null.
при добавлении их в список. Значит ли это, что я не преобразовываю их в строки? Можете ли вы заметить, что я делаю неправильно?
Как всегда большое спасибо за ваше время и помощь.
Это метод:
Future<List<String>> getCityDb(String isoLocationDb) async {
print('getCityDb called');
// namePrefix
final String request =
'https://wft-geo-db.p.rapidapi.com/v1/geo/cities?namePrefix=bologna&languageCode=IT'; //&types=ADM2'; // working
var response = await get(Uri.encodeFull(request), headers: {
'x-rapidapi-host': 'wft-geo-db.p.rapidapi.com',
'x-rapidapi-key': apiKey,
});
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body);
List<dynamic> properties = jsonResponse['data'];
print('properties are $properties');
String name = properties.first['name'].toString();
String region = properties.first['region'].toString();
String country = properties.first['country'].toString();
// print(' getCityDb ${jsonResponse.runtimeType} : $jsonResponse');
print('getCityDb jsonResponse name is :$name');
print('getCityDb jsonResponse region is :$region');
print('getCityDb jsonResponse country is: $country');
List<String> cityDb;
cityDb.add(name);
cityDb.add(region);
cityDb.add(country);
return cityDb;
} else {
print('getCityDB() Request failed with status: ${response.statusCode}.');
}
}
И это консольная печать:
flutter: getCityDb called
flutter: properties are [{id: 148826, wikiDataId: Q1891, type: CITY, city: Bologna, name: Bologna, country: Italia, countryCode: IT, region: Emilia-Romagna, regionCode: 45, latitude: 44.493888888, longitude: 11.342777777}, {id: 3029926, wikiDataId: Q18488439, type: CITY, city: Bolognana, name: Bolognana, country: Italia, countryCode: IT, region: Toscana, regionCode: 52, latitude: 44.04018, longitude: 10.47244}, {id: 63950, wikiDataId: Q50839, type: CITY, city: Bolognano, name: Bolognano, country: Italia, countryCode: IT, region: Abruzzo, regionCode: 65, latitude: 42.216666666, longitude: 13.966666666}, {id: 66955, wikiDataId: Q18478734, type: CITY, city: Bolognano-Vignole, name: Bolognano-Vignole, country: Italia, countryCode: IT, region: Trentino-Alto Adige, regionCode: 32, latitude: 45.91196, longitude: 10.90497}]
flutter: getCityDb jsonResponse name is :Bologna
flutter: getCityDb jsonResponse region is :Emilia-Romagna
flutter: getCityDb jsonResponse country is: Italia
flutter: Erros is NoSuchMethodError: The method 'add' was called on null.