List NoSuchMethodError:向列表中添加值时,对null调用了方法“add”

List NoSuchMethodError:向列表中添加值时,对null调用了方法“add”,list,http,flutter,dart,List,Http,Flutter,Dart,我从API请求中获取值,并将它们存储到字符串变量中,以添加到列表中。当打印这些变量时,打印是正常的,但是我得到了NoSuchMethodError:方法'add'被调用为null。将它们添加到列表中时。 这是否意味着我没有真正将它们转换为字符串? 你能看出我做错了什么吗 一如既往,非常感谢您的时间和帮助 方法如下: Future<List<String>> getCityDb(String isoLocationDb) async { print('getCity

我从API请求中获取值,并将它们存储到字符串变量中,以添加到列表中。当打印这些变量时,打印是正常的,但是我得到了NoSuchMethodError:方法'add'被调用为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}.');
    }
  }

首先初始化您的列表

List<String> cityDb = new List<String>();

首先初始化您的列表

List<String> cityDb = new List<String>();

错误消息给出了确切的错误:add方法是在null上调用的

在cityDb上调用add方法,这意味着cityDb为null

更改此项:

List<String> cityDb = new List<String>();

错误消息给出了确切的错误:add方法是在null上调用的

在cityDb上调用add方法,这意味着cityDb为null

更改此项:

List<String> cityDb = new List<String>();

首先,这里您需要初始化列表,然后添加值,尝试如下操作:-

List<String> cityDb=List();
cityDb.add(name);
cityDb.add(region);
cityDb.add(country);

首先,这里您需要初始化列表,然后添加值,尝试如下操作:-

List<String> cityDb=List();
cityDb.add(name);
cityDb.add(region);
cityDb.add(country);

当然我没有草签名单!谢谢看不到它..在thing Frook上,List cityDb=new List;超过列表cityDb=[];意味着每次调用该方法时,我都不需要清除列表,对吗?当然。。我没有草签名单!谢谢看不到它..在thing Frook上,List cityDb=new List;超过列表cityDb=[];意味着每次调用该方法时,我都不需要清除列表,对吗?谢谢。。我看不出这是公平的,我会在Muldec第一个进来的时候给他打分。再次非常感谢。。我看不出这是公平的,我会在Muldec第一个进来的时候给他打分。再次非常感谢。