flutter - 该方法是在 null 发生的偶然事件上调用的
问题描述
显示以下代码:
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
"list_page": (context) => PoiList(),
"distance_page": (context) => Distance()
},
home: HomePage()
);
}
}
class HomePage extends StatelessWidget{
AMapController _aMapController;
BuildContext _buildContext;
double _clickedLat;
double _clickedLng;
void onAMapCreated(AMapController controller) {
_aMapController = controller;
}
void onPoiNaviButtonClicked(double lat, double lng) async{
print(lat);
print(lng);
_clickedLat = lat;
_clickedLng = lng;
caculate(lat, lng);
}
void caculate(double lat, double lng) async{
if (_aMapController == null){
print("_aMapController == null");
}
_aMapController.caculteDistanceBetweenTwoPois({"lat":lat, "lng":lng}); //This is the error codes.
}
void onDidCaculateDistance(int distance){
Navigator.of(_buildContext).pushNamed("distance_page", arguments: {"distance":distance, "poi":{"lat": _clickedLat,"lng":_clickedLng}});
}
List<Map<String, double>> pois = [
{'lng': 119.932382, 'lat': 31.615883},
{'lng': 119.934098, 'lat': 31.619684},
{'lng': 119.946801, 'lat': 31.608574},
{'lng': 119.957788, 'lat': 31.589859},
{'lng': 119.949548, 'lat': 31.628162}
];
@override
Widget build(BuildContext context) {
print("Widget build");
_buildContext = context;
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Stack(
fit: StackFit.expand, //未定位的widget占满stack
children: <Widget>[
AMap(onAMapCreated: onAMapCreated, onPoiNaviButtonClicked: onPoiNaviButtonClicked, onDidCaculateDistance:onDidCaculateDistance),
Positioned(
child: RaisedButton(
color: Colors.orange,
onPressed: () {
print("定位");
},
child: Text("定位"),
),
right: 20,
bottom: 60),
Positioned(
child: RaisedButton(
color: Colors.orange,
onPressed: () {
print("缩小");
_aMapController == null
? null
: _aMapController.setZoomLevel(6);
},
child: Text("缩小")),
right: 20,
bottom: 120),
Positioned(
child: RaisedButton(
color: Colors.orange,
onPressed: () {
print("放大");
_aMapController == null
? null
: _aMapController.setZoomLevel(10);
},
child: Text("放大")),
right: 20,
bottom: 180),
Positioned(
child: RaisedButton(
color: Colors.orange,
onPressed: () {
print("搜索");
//Flutter中准备poi点经纬度坐标数组,在原生地图上显示
_aMapController == null ? null : _aMapController.showNearByChargingStation(pois);
},
child: Text("搜索")),
right: 20,
bottom: 240),
Positioned(
child: RaisedButton(
color: Colors.orange,
onPressed: () {
// jump(pois, distance)
print("跳转");
// Navigator.of(context).pushNamed("list_page", arguments: {'pois': pois, 'mapController':_aMapController});
},
child: Text("跳转")),
right: 20,
bottom: 300),
],
));
}
}
在 HomePage(StatelessWidget) 中,我定义了一个变量:AMapController _aMapController; 在构建函数中,我创建了一个 AMap 小部件(这是 iOS 原生 mapView)。高地图创建时。下面的回调将被调用,_aMapController 变量将被初始化。
void onAMapCreated(AMapController controller) {
_aMapController = controller;
}
onPoiNaviButtonClicked() 函数是另一个由 ios 原生代码触发的回调。在 onPoiNaviButtonClicked() 函数中,我调用 caculate() 函数。在 caculate() 函数中。_aMapController 有时会为空!很奇怪。不是每次都发生!它只是偶尔发生?为什么!
而且好像跳转到secondpage(Distance()),然后返回首页,有时候会这样!
解决方案
推荐阅读
- angular - 需要为 ngx-editor 安装 ngx-boostrap 版本 4
- php - 在 SELECT 中包含一个实际上不在数据库 laravel 中的列
- reactjs - 语义 UI React 侧边栏仅在可见性更改时呈现推送器(redux/rematch)
- python - 如何将有序变量保存到文件中
- django - 为什么 django rest-frame 工作的序列化程序中的外键字段变为只读?
- euclidean-distance - 欧几里得距离除以数组数量的平方根?
- c++ - c++线程,重复/丢失线程
- javascript - 无法使用 for/while LOOP Javascript/Jquery 获得最终结果
- java - 使用Apache poi在特定列中查找excel中的最后一行没有
- javascript - javascript:保存表值以用作默认值