我正试图运行一个特定的代码来查找地震,但当我运行它时,我得到了"方法"Cast"是在null"上调用的;它似乎不起作用,我不知道这个特定的错误是否是原因,因为即使我找到了解决这个问题的方法,我仍然会收到一个"资源无法调用释放错误"。这是错误来自的json模型
class EarthquakeModel {
String? type;
Metadata? metadata;
List<Features>? features;
List<double>? bbox;
EarthquakeModel({this.type, this.metadata, this.features, this.bbox});
EarthquakeModel.fromJson(Map<String, dynamic> json) {
type = json['type'];
metadata = json['metadata'] != null
? new Metadata.fromJson(json['metadata'])
: null;
if (json['features'] != null) {
features = <Features>[];
json['features'].forEach((v) {
features!.add(new Features.fromJson(v));
});
}
bbox = json['bbox'].cast<double>();
}
特别是烤肉。
这是我试图运行的代码
class QuakeApp extends StatefulWidget {
const QuakeApp({Key? key}) : super(key: key);
@override
State<QuakeApp> createState() => _QuakeAppState();
}
class _QuakeAppState extends State<QuakeApp> {
late Future<EarthquakeModel> quakeData;
Completer<GoogleMapController> _controller = Completer();
List<Marker> _markerList = <Marker>[];
@override
void initState() {
// TODO: implement initState
super.initState();
quakeData = Network().getEarthquake();
//quakeData.then((value) => print('place: ${value.features?[0].properties?.place}'));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
BuildGoogleMap(),
],
),
floatingActionButton: FloatingActionButton.extended(
onPressed: (){
setState(() {
_markerList.clear();
setState(() {
quakeData.then((quake) => {
quake.features?.forEach((element) {
_markerList.add(Marker(markerId: MarkerId(element.id??''),
infoWindow: InfoWindow(
title: element.properties?.mag?.toString(),
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueMagenta),
position: LatLng(element.geometry?.coordinates?[1]?? 0, element.geometry?.coordinates?[0]?? 0),
onTap: (){}
)
);
})
});
});
});
},
label: Text('Find Quakes')
),
);
这就是BuidGoogleMap方法
class BuildGoogleMap extends StatefulWidget {
const BuildGoogleMap({Key? key}) : super(key: key);
@override
State<BuildGoogleMap> createState() => _BuildGoogleMapState();
}
class _BuildGoogleMapState extends State<BuildGoogleMap> {
Completer<GoogleMapController> _controller = Completer();
List<Marker> _markerList = <Marker>[];
@override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(36.1083333, 117.8608333),
zoom: 3
),
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller){
_controller.complete(controller);
},
markers: Set<Marker>.of(_markerList),
),
);
}
}
最好检查数据是否为null。
尝试
final data = json['bbox'] as List?;
if (data != null) {
bbox = data.map((e) => double.tryParse(e) ?? 0.0).toList();
}