如何在OnPressed中调用多个函数

问题描述 投票:0回答:1
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正在工作

_animateToUser() async {
    var pos = await location.getLocation();
    mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
        target: LatLng(pos.latitude, pos.longitude), zoom: 17.0)));
  }

_ addGeoPoint不起作用

Future<DocumentReference> _addGeoPoint() async {
    var pos = await location.getLocation();
    final coordinates = Coordinates(pos.latitude, pos.longitude);
    var addresses =
        await Geocoder.local.findAddressesFromCoordinates(coordinates);
    var first = addresses.first;
    print("${first.featureName} : ${first.addressLine}");
//    GeoFirePoint point =
//        geo.point(latitude: pos.latitude, longitude: pos.longitude);
    return _firestore.collection('location').add({
      'UserLocation': first.featureName,
      'UserLocationName': first.addressLine
    });
  }

但是当我更改onPressed为

onPressed: _addGeopoint,

_ addGeopoint起作用。

我无法一次调用这两个功能。

flutter dart flutter-layout flutter-dependencies flutter-test
1个回答
0
投票

当您在函数中使用async并等待时,需要将try / catch并将return语句放入catch中。万一发生任何异常或由于其他一些原因而导致它无法继续前进,它将不会调用第二个函数。请检查以下示例是否相同。

© www.soinside.com 2019 - 2024. All rights reserved.