internetSpeedTestPlugin method

dynamic internetSpeedTestPlugin()

Implementation

internetSpeedTestPlugin() async {
  reset();
  await internetSpeedTest.startTesting(
    onStarted: () {
      reset();
      print('onStarted');
    },
    onCompleted: (TestResult download, TestResult upload) {
      print(
          'the transfer rate ${download.transferRate}, ${upload.transferRate}');

      _downloadRate = download.transferRate;
      _unitText = download.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
      _downloadProgress = '100';
      _downloadCompletionTime = download.durationInMillis;

      _uploadRate = upload.transferRate;
      _unitText = upload.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
      _uploadProgress = '100';
      _uploadCompletionTime = upload.durationInMillis;

      resDownload = (downloadRate / stepsDown).toStringAsFixed(2);
      print('[$TAG] : resdownload $resDownload');

      resUpload = (uploadRate / stepsUp).toStringAsFixed(2);
      print('[$TAG] : resUpload $resUpload');

      // storeToCit();
    },
    onProgress: (double percent, TestResult data) {
      _unitText = data.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
      if (data.type == TestType.DOWNLOAD) {
        _downloadRate = data.transferRate;
        _downloadProgress = percent.toStringAsFixed(2);
        stepsDown++;
        downloadRate += _downloadRate;

        print(
            'Download : the transfer rate $_downloadRate, the percent $_downloadProgress');
      } else {
        _uploadRate = data.transferRate;
        _uploadProgress = percent.toStringAsFixed(2);
        stepsUp++;
        uploadRate += _uploadRate;

        print(
            'Upload : the transfer rate $_uploadRate, the percent $_uploadProgress');
      }
    },
    onError: (String errorMessage, String speedTestError) {
      print(
          'the errorMessage $errorMessage, the speedTestError $speedTestError');

      reset();
    },
    onDefaultServerSelectionInProgress: () {
      _isServerSelectionInProgress = true;

      print('onDefaultServerSelectionInProgress');
    },
    onDefaultServerSelectionDone: (Client? client) {
      print('onDefaultServerSelectionDone');

      _isServerSelectionInProgress = false;
      _ip = client?.ip;
      _asn = client?.asn;
      _isp = client?.isp;
    },
    onDownloadComplete: (TestResult data) {
      _downloadRate = data.transferRate;
      _unitText = data.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
      _downloadCompletionTime = data.durationInMillis;

      print('[$TAG] : average ${downloadRate / stepsDown}');

      print(
          'onDownloadComplete : the transfer rate $_downloadRate, the percent $_downloadCompletionTime step $stepsDown');
    },
    onUploadComplete: (TestResult data) {
      _uploadRate = data.transferRate;
      _unitText = data.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
      _uploadCompletionTime = data.durationInMillis;

      print('[$TAG] : average ${uploadRate / stepsUp}');

      print(
          'onUploadComplete : the transfer rate $_uploadRate, the percent $_uploadCompletionTime step $stepsUp');
    },
  );
}