【问题标题】:NoSuchMethodError: The method 'add' was called on null when adding a value to a list FlutterNoSuchMethodError:将值添加到列表 Flutter 时,在 null 上调用了方法“add”
【发布时间】:2020-06-28 08:17:31
【问题描述】:

我从 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.

【问题讨论】:

    标签: list http flutter dart


    【解决方案1】:

    首先初始化您的列表。

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

    【讨论】:

    • 谢谢.. 我看不到它
    • 公平地说,我会标记 Muldec 的,因为它首先出现。再次非常感谢您。
    【解决方案2】:

    错误消息告诉您究竟出了什么问题:add 方法是在 null 上调用的。

    您在cityDb 上调用add 方法,这意味着cityDb 为空。

    改变这个:

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

    【讨论】:

    • 当然.. 我没有初始化列表!谢谢..看不到..
    • 在事情面团上,List&lt;String&gt; cityDb = new List&lt;String&gt;();over List&lt;String&gt; cityDb = []; 意味着在每次调用该方法时我都不需要清除列表对吗?
    【解决方案3】:

    首先,这里你需要初始化列表然后添加值,试试这样:-

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

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 1970-01-01
      • 2020-10-12
      • 2020-04-27
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多