Reputation: 6358
I'm getting values from an API request, and store them into String
variables to be added to a List<String>
. When printing this variables prints are fine, but then I get NoSuchMethodError: The method 'add' was called on null.
when adding them to the list.
Does it mean that I'm not really converting them into strings?
Can you spot what I'm doing wrong ?
As always many thanks for your time and help.
This is the method:
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}.');
}
}
And this is the console print:
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.
Upvotes: 1
Views: 87
Reputation: 257
firstly, here you need to initialize list and then add value , try like this:-
List<String> cityDb=List();
cityDb.add(name);
cityDb.add(region);
cityDb.add(country);
Upvotes: 2
Reputation: 4901
The error message gives you exactly what's wrong : the add
method was called on null
.
You call the add
method on cityDb
, which means that cityDb
is null.
Change this :
List<String> cityDb = new List<String>();
Upvotes: 2
Reputation: 514
initialize your List first.
List<String> cityDb = new List<String>();
Upvotes: 3