Как вызвать несколько функций в OnPressed - PullRequest
0 голосов
/ 06 февраля 2020
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: _addGeopoint,

_addGeopoint работает.

Я не могу вызвать обе функции один раз.

1 Ответ

0 голосов
/ 06 февраля 2020

Поскольку вы используете asyn c и ожидаете внутри функции, вам нужно поместить try / catch и оператор return в catch. В случае возникновения какого-либо исключения или по каким-либо другим причинам он не может двигаться дальше, он не будет вызывать вторую функцию. Пожалуйста, проверьте ниже пример для того же.

onPressed: () {
     _funOne();
     _funTwo();
}),


_funOne() async{
  try {
    await location.getLocation()(); 
    // your code...
  } catch (e) {
    return;
  }
}

_funTwo() async{
  try {
    await location.getLocation()(); 
    // your code...
  } catch (e) {
    return;
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...