Flutter应用程序错误“ NoSuchMethodError:在空值上调用了吸气剂'length'”

 收藏

正在收到此错误,找不到原因,该应用程序显示了一个从Google Maps地方api获得的Google Maps呼叫医院,在chrome中使用时api调用返回正常,但该应用程序无法构建并显示“ NoSuchMethodError:getter'length '被调用为null”错误。谢谢你的帮助。


class Search extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final currentPosition = Provider.of<Position>(context);
    final placesProvider = Provider.of<Future<List<Place>>>(context);

    return FutureProvider(
      create: (context) => placesProvider,
      child: Scaffold(
        body: (currentPosition != null)
            ? Consumer<List<Place>>(
                builder: (_, places, __) {
                  return Column(
                    children: <Widget>[
                      Container(
                        height: MediaQuery.of(context).size.height / 3,
                        width: MediaQuery.of(context).size.width,
                        child: GoogleMap(
                          initialCameraPosition: CameraPosition(
                              target: LatLng(currentPosition.latitude,
                                  currentPosition.longitude),
                              zoom: 16.0),
                          zoomGesturesEnabled: true,
                          mapToolbarEnabled: true,
                          myLocationEnabled: true,
                          scrollGesturesEnabled: true,
                        ),
                      ),
                      SizedBox(
                        height: 10.0,
                      ),
                      Expanded(
                        child: ListView.builder(
                            itemCount: places.length,
                            itemBuilder: (context, index) {
                              return Card(
                                child: ListTile(
                                  title: Text(places[index]?.name),
                                ),
                              );
                            }),
                      )
                    ],
                  );
                },
              )
            : Center(
                child: CircularProgressIndicator(),
              ),
      ),
    );
  }
}

在chrome中调用api请求时,效果很好,问题出在构建应用程序时。这是我的places_service.dart文件:


class PlacesService {
  final key = 'AIzaSyD-vLS7lrciD2Qzcy-0Q_hkKJy1GOR_DnE';

  Future<List<Place>> getPlaces(double lat, double lng) async {
    var response = await http.get('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$lng&type=hospital&rankby=distance&key=$key');
    var json = convert.jsonDecode(response.body);
    var jsonResults = json['results'] as List;
    return jsonResults.map((place) => Place.fromJson(place)).toList();
  }

}

问题是这一行:

itemCount: places.length,
回复