Home > Software design >  How to get the current internet speed ? | Flutter
How to get the current internet speed ? | Flutter

Time:09-07

I want to know the downlink speed. It's not that I want to download something, I just want to know the speed

CodePudding user response:

First add dependency

dependencies: internet_speed_test: ^1.5.0

now the speed test code

speed test code part

CodePudding user response:

Plugin is available for android and ios Plugin can be use

import 'package:internet_speed_test/internet_speed_test.dart';

  final internetSpeedTest = InternetSpeedTest();

  internetSpeedTest.startDownloadTesting(
     onDone: (double transferRate, SpeedUnit unit) {
        // TODO: Change UI
     },
     onProgress: (double percent, double transferRate, SpeedUnit unit) {
        // TODO: Change UI
     },
     one rror: (String errorMessage, String speedTestError) {
        // TODO: Show toast error
     },
   );



  internetSpeedTest.startUploadTesting(
     onDone: (double transferRate, SpeedUnit unit) {
       print('the transfer rate $transferRate');
       setState(() {
         // TODO: Change UI
       });
     },
     onProgress: (double percent, double transferRate, SpeedUnit unit) {
       print(
           'the transfer rate $transferRate, the percent $percent');
       setState(() {
         // TODO: Change UI
       });
     },
     one rror: (String errorMessage, String speedTestError) {
       // TODO: Show toast error
     },
  );

CodePudding user response:

var tempDir = await getTemporaryDirectory();
    CancelToken token = CancelToken();

    DateTime? startTime;
    double? speed;
      await Dio().download('${widget.videoUrl}', "${tempDir.path}/deer",
          cancelToken: token,
          options: Options(headers: {HttpHeaders.acceptEncodingHeader: "*"}),
          // disable gzip
          // Listen the download progress.
          onReceiveProgress: (received, total) {
        if (total != -1) {
          startTime ??= DateTime.now();
          if (received > 1024 * 1024 || received == total) {
            token.cancel("cancelled");
            Duration downloadTime = DateTime.now().difference(startTime!);
        
            speed = received / 1024 / (downloadTime.inMicroseconds) * 1000000;
          }
        }
      });
  • Related