Flutter 如何在OnPressed中调用多个函数
仅限用户工作Flutter 如何在OnPressed中调用多个函数,flutter,dart,flutter-layout,flutter-dependencies,flutter-test,Flutter,Dart,Flutter Layout,Flutter Dependencies,Flutter Test,仅限用户工作 Positioned( bottom: 25, right: 10, child: FloatingActionButton( heroTag: 'tag1', backgroundColor: Colors.white.withOpacity(0.7), child: Icon( Icons.locatio
Positioned(
bottom: 25,
right: 10,
child: FloatingActionButton(
heroTag: 'tag1',
backgroundColor: Colors.white.withOpacity(0.7),
child: Icon(
Icons.location_searching,
color: Colors.purple,
),
onPressed: () {
_addGeoPoint();
_animateToUser();
}),
),
_animateToUser() async {
var pos = await location.getLocation();
mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(pos.latitude, pos.longitude), zoom: 17.0)));
}
\u添加地质点不工作
Positioned(
bottom: 25,
right: 10,
child: FloatingActionButton(
heroTag: 'tag1',
backgroundColor: Colors.white.withOpacity(0.7),
child: Icon(
Icons.location_searching,
color: Colors.purple,
),
onPressed: () {
_addGeoPoint();
_animateToUser();
}),
),
_animateToUser() async {
var pos = await location.getLocation();
mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(pos.latitude, pos.longitude), zoom: 17.0)));
}
这个地球点很有效
我不能一次调用这两个函数。因为在函数中使用async和Wait,所以需要将try/catch和put return语句放在catch中。如果发生任何异常或由于其他原因无法进一步移动,它将不会调用第二个函数。请检查下面的示例以了解相同的信息
onPressed: _addGeopoint,
尝试此
onPressed:()=>[\u addGeoPoint(),\u animateToUser()]
尝试将onPressed
回调转换为async
并在两种方法之前使用wait
关键字。类似于按下按钮:()异步{await _addGeoPoint();await _animateToUser()}@AbhilashChandran,这确实有效。但加载需要时间。还有别的快餐吗option@RahulChokshi我试过了,但没有work@AsbahRiyas调用的持续时间取决于您在这些函数中执行的操作。这只能通过计时这些执行来识别,就像在这些方法中等待之前和之后的打印时间一样,以查看计时。我看到的一件事是,您正在调用await location.getLocation()两种方法中的代码>。也许您可以将其移出并将此信息传递给这两个方法,从而避免一个长调用。