tryNetwork method

Future tryNetwork({
  1. Duration? timeout,
  2. required SimulatedRequestType requestType,
})

checks whether a connection to the network is allowed throws NoConnectionToBackendException or SocketException if its not.

Implementation

Future tryNetwork({
  Duration? timeout,
  required Helper.SimulatedRequestType requestType,
}) async {
  //check network
  if (Options().forceOffline)
    throw NoConnectionToBackendException(
        S.current!.nonetwork_forcedOfflineMode);
  final connection = await (Connectivity().checkConnectivity());
  if (connection == ConnectivityResult.none)
    throw NoConnectionToBackendException(S.current!.noNetworkAvailable);
  if (connection == ConnectivityResult.mobile &&
      ((requestType != Helper.SimulatedRequestType.GET &&
              !Options().useMobileNetworkForUpload) ||
          !Options().useMobileNetworkForDownload))
    throw NoConnectionToBackendException(S.current!.mobileNetworkNotAllowed);
}