为什么在尝试调用中显示字符串传递时给出NoSuchMethodFound错误?

时间:2019-01-24 20:42:14

标签: dart flutter

我正在从Google Map api访问地址,但无法将其添加到列表中。打印时,它正在打印输出,但未将其添加到列表address1 List中。 我已经试过几次了,但是没有用。

import 'dart:convert';
import 'package:ambulance_service/models/placeDetails.dart';
import 'package:http/http.dart' as http;

const kGoogleApiKey = "MyGoogleMapKey";
List address1;
Future<List> getPlaces(double lat, double long) async {
String apiUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=1500&type=hospital&keyword=hospital&key=$kGoogleApiKey";
http.Response response = await http.get(apiUrl);
var places = <PlaceDetail1>[];
List data = json.decode(response.body)['results'];
int i = 0;
await wow(data);
data.forEach((f) =>
   places.add(new PlaceDetail(
      f['icon'], f['rating'].toString(), f['name'], f['place_id'])));

return places;
}

 Future<String> getPlaceAddress(String placeId) async {
  String apiUrl = "https://maps.googleapis.com/maps/api/place/details/json?                
placeid=$placeId&key=$kGoogleApiKey";
http.Response response = await http.get(apiUrl);
Map<dynamic,dynamic> data = json.decode(response.body)['result'];
return data['formatted_address'].toString();
 }

Future<String> getPlaceMob(String placeId) async {
String apiUrl = "https://maps.googleapis.com/maps/api/place/details/json? 
 placeid=$placeId&key=$kGoogleApiKey";
http.Response response = await http.get(apiUrl);
Map<dynamic,dynamic> data = json.decode(response.body)['result'];
return data['formatted_address'].toString();
}

wow(List a)async{
for(var data in a){
String a =await getPlaceAddress(data['place_id']);
print(a);
address1.add(a);
 }
}




                          PLaceDetail is like:






 class PlaceDetail1{
 String icon,name,place_id,rating;

   PlaceDetail1(this.icon,this.rating,this.name,this.place_id);

 }

class PlaceDetail extends PlaceDetail1{
 String formatted_address,formatted_phone_number;

  PlaceDetail(String icon,String name,String place_id,String rating,[this.formatted_address,this.formatted_phone_number]):super(icon,name,place_id,rating);

 }



                           Errors:


I/flutter (18570): 01, Zoo Rd, Azad Nagar, Nawabganj, Kanpur, Uttar Pradesh 208002, India
E/flutter (18570): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter (18570): NoSuchMethodError: The method 'add' was called on null.
E/flutter (18570): Receiver: null
E/flutter (18570): Tried calling: add("01, Zoo Rd, Azad Nagar, Nawabganj, Kanpur, Uttar Pradesh 208002, India")
E/flutter (18570): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter (18570): #1      wow (package:ambulance_service/data_from_api/google_maps_api_data.dart:48:14)
E/flutter (18570): <asynchronous suspension>
E/flutter (18570): #2      getPlaces (package:ambulance_service/data_from_api/google_maps_api_data.dart:19:9)
E/flutter (18570): <asynchronous suspension>

在上述错误中: 据说方法'add'在null上被调用并表明它尝试了 尝试调用:添加(“印度,北方邦,纳瓦布甘杰,阿扎德·纳加尔,Zoo Rd 01,208002,印度”) 这表明试图在add中传递一个字符串,但它显示为null。

2 个答案:

答案 0 :(得分:0)

您有List address1;,它从未初始化为列表值,因此它的值为null。在错误消息“方法'add'在null上被调用”中,“被称为 on ”表示左侧-因此address1为null。

尝试var address1 = <String>[];(或final address1 = <String>[])初始化为空列表,以便.add()调用可以成功。

答案 1 :(得分:0)

发生这种情况是因为您尚未初始化var post = document.querySelectorAll('justify.-column'); ,因此它是一个var post = document.querySelectorAll('justify.-column:nth-of-type(1n + 7)'); 指针,并且您正在尝试对其调用List

只需将null替换为add