dial method

Future<void> dial(
  1. String number, {
  2. bool retryWithFaceTime = false,
  3. bool useFaceTimeAudio = false,
})

Dials a phone number directly.

Uses the Intent package for the Android implementation.

Implementation

Future<void> dial(
  String number, {
  bool retryWithFaceTime = false,
  bool useFaceTimeAudio = false,
}) async {
  final phonePermission = await Permission.phone.status;

  if (Platform.isAndroid) {
    final intent = AndroidIntent(
      action: 'android.intent.action.CALL',
      data: 'tel:$number',
    );
    if (phonePermission.isDenied) {
      Permission.phone.request().then((status) {
        if (status.isGranted) {
          intent.launch();
        }
      });
    }
    if (phonePermission.isGranted) {
      try {
        await intent.launch();
      } catch (e) {
        print(e);
      }
    }
  }

  if (Platform.isIOS || onIpad) {
    // Check cellular or wifi
    final connectivityResult = await _connectivityCheck();
    // Check if physical device - emulators can't make calls
    if (_iosDeviceInfo.isPhysicalDevice) {
      // If cellular, make a call as normal
      try {
        if (connectivityResult == ConnectivityResult.mobile) {
          await _channel.invokeMethod(
            'dial',
            <String, Object>{'number': number},
          );
        }
      } catch (e) {
        if (retryWithFaceTime) {
          await dialFaceTime(number, useFaceTimeAudio);
        }
      }
    } else if (_iosDeviceInfo.isPhysicalDevice &&
        connectivityResult == ConnectivityResult.wifi) {
      await dialFaceTime(number, useFaceTimeAudio);
    } else {
      throw 'This action is not available on the iOS simulator.';
    }
  }

  if (Platform.isMacOS) {
    await dialFaceTime(number, useFaceTimeAudio);
  }
}