getNetworkInfo method

  1. @override
Future<NetworkInfo> getNetworkInfo()
override

Gets current network information.

Returns NetworkInfo with details about the current network connection including type, speed, and interface information.

Implementation

@override
Future<NetworkInfo> getNetworkInfo() async {
  final navigator = web.window.navigator;
  var connectionType = 'unknown';
  var networkSpeed = 'Unknown';

  // Try to access NetworkInformation API
  final connection = (navigator as NavigatorWithConnection).connection;
  if (connection != null) {
    // Try to get specific type (chrome/edge specific) primarily for ethernet
    final specificType = connection.type;
    if (specificType != null && specificType != 'unknown') {
      connectionType = specificType;
    } else {
      // Fallback to effectiveType if specific type is not available
      final type = connection.effectiveType;
      if (type != null) {
        // effectiveType returns 'slow-2g', '2g', '3g', or '4g'
        // This describes speed/quality, not strictly the interface,
        // but it's the best we have if .type is missing.
        connectionType = type;
      }
    }

    // Get downlink speed
    final downlink = connection.downlink;
    if (downlink != null) {
      networkSpeed = '$downlink Mbps';
    }
  }

  // Basic online/offline check as fallback or supplement
  final isConnected = navigator.onLine;
  if (isConnected && connectionType == 'unknown') {
    connectionType = 'wifi'; // Fallback assumption
  }

  return NetworkInfo(
    connectionType: connectionType,
    networkSpeed: networkSpeed,
    isConnected: isConnected,
    ipAddress: await _getLocalIpAddress(),
    // MAC address is strictly blocked by browsers for security
    // (to prevent tracking). It is impossible to retrieve via JS.
    macAddress: 'Not Available',
  );
}