Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在OnPressed中调用多个函数_Flutter_Dart_Flutter Layout_Flutter Dependencies_Flutter Test - Fatal编程技术网

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()。也许您可以将其移出并将此信息传递给这两个方法,从而避免一个长调用。