MissingPluginException in my flutter code - NetworkInfo().getLocationServiceAuthorization()

389 Views Asked by At

I've written a Class to handle network connection. I need because my app works only in a specific environment so I want to check the wifi SSID.

I use the connectivity package to check if I'm connected to WIFI.

I want to use the network_info_plus package to read the wifi SSID name, but it throws this exception:

MissingPluginException (MissingPluginException(No implementation found for method getLocationServiceAuthorization on channel dev.fluttercommunity.plus/network_info))

My code is the following:

import 'dart:async';

import 'package:network_info_plus/network_info_plus.dart' as info;
import 'package:connectivity/connectivity.dart';
import 'package:flutter/services.dart';

enum ConnectivityStatus { Connected, NotConnected }

class ConnectivityService {
  // Create our public controller
  StreamController<ConnectivityStatus> connectionStatusController =
      StreamController<ConnectivityStatus>();

  ConnectivityService() {
    // Subscribe to the connectivity Chanaged Steam
    Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      // Use Connectivity() here to gather more info if you need t

      connectionStatusController.add(_getStatusFromResult(result));
    });
  }

  // Convert from the third part enum to our own enum
  ConnectivityStatus _getStatusFromResult(ConnectivityResult result) {
    switch (result) {
      case ConnectivityResult.mobile:
        return ConnectivityStatus.NotConnected;
      case ConnectivityResult.wifi:
        _initNetworkInfo();
        return ConnectivityStatus.Connected;
      case ConnectivityResult.none:
        return ConnectivityStatus.NotConnected;
      default:
        return ConnectivityStatus.NotConnected;
    }
  }
}

Future<String?> _initNetworkInfo() async {
  String? wifiName;
  final _networkInfo = info.NetworkInfo();

  try {
    var status = await _networkInfo.getLocationServiceAuthorization();
    if (status == info.LocationAuthorizationStatus.notDetermined) {
      status = await _networkInfo.requestLocationServiceAuthorization();
    }
    if (status == info.LocationAuthorizationStatus.authorizedAlways ||
        status == info.LocationAuthorizationStatus.authorizedWhenInUse) {
      wifiName = await _networkInfo.getWifiName();
    } else {
      wifiName = await _networkInfo.getWifiName();
    }
  } on PlatformException catch (e) {
    print(e.toString());
    wifiName = 'Failed to get Wifi Name';
  }

  return wifiName;
}

EDIT: I decided to create a class to handle the retrieval of wifi name:

...
      case ConnectivityResult.wifi:
        if (NetworkInfo().wifiName == 'WIFI_SSID_NAME') // !!! HANDLE ASYNC
          return ConnectivityStatus.Connected;
        else
          return ConnectivityStatus.NotConnected;
...

class NetworkInfo {
  String? wifiName;
  NetworkInfo() {
    _initNetworkInfo();
  }

  Future<void> _initNetworkInfo() async {
    String? wifiName;
    final _networkInfo = info.NetworkInfo();

    try {
      var status = await _networkInfo.getLocationServiceAuthorization();
      if (status == info.LocationAuthorizationStatus.notDetermined) {
        status = await _networkInfo.requestLocationServiceAuthorization();
      }
      if (status == info.LocationAuthorizationStatus.authorizedAlways ||
          status == info.LocationAuthorizationStatus.authorizedWhenInUse) {
        wifiName = await _networkInfo.getWifiName();
      } else {
        wifiName = await _networkInfo.getWifiName();
      }
    } on PlatformException catch (e) {
      print(e.toString());
      wifiName = 'Failed to get Wifi Name';
    }

    this.wifiName = wifiName;
  }
}
1

There are 1 best solutions below

0
On

The getLocationServiceAuthorization is only for iOS so if you are coding for Android too, you should split them into separate blocks:

final _networkInfo = info.NetworkInfo();

try {
  //may throw on non-iOS platforms
  var status = await _networkInfo.getLocationServiceAuthorization();
  if (status == info.LocationAuthorizationStatus.notDetermined) {
    status = await _networkInfo.requestLocationServiceAuthorization();
  }
} on PlatformException catch (e) {
  print(e.toString());
  wifiName = 'Failed to verify authorization';
}

try {
  wifiName = await _networkInfo.getWifiName();
} on PlatformException catch (e) {
  print(e.toString());
  wifiName = 'Failed to get Wifi Name';
}

Also inside /android/app/build.gradle you should insert:

shrinkResources false
minifyEnabled false

in the buildTypes/release block:

buildTypes {
    release {
        shrinkResources false
        minifyEnabled false