How to force flutter to use wifi rather than 4G?

1,704

Solution 1

Core Flutter framework has not that feature yet(and won't have at least for a long time imo).

When it comes hardware related things, you can almost say that native code is only way to go. Good thing is there many official & third party packages that already done the job for you.

For example this package is popular one for connection related features: https://pub.dev/packages/connectivity

This is the example code you need:

import 'package:connectivity/connectivity.dart';

var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
  // I am connected to a mobile network.
} else if (connectivityResult == ConnectivityResult.wifi) {
  // I am connected to a wifi network.
}

Solution 2

There is a plugin that helps you to solve the problem.

You need to add this https://github.com/alternadom/WiFiFlutter plugin to your project like so:

wifi_iot:
    git: https://github.com/alternadom/WiFiFlutter.git

and then to force wifi usage you call

  WiFiForIoTPlugin.forceWifiUsage(true);

I made it safe and added a platform check

import 'dart:io' show Platform;
if(Platform.isAndroid) {
   WiFiForIoTPlugin.forceWifiUsage(true);
}

The downside is, that at the first time you do this in your app, the user will be redirected to allow the app to change system settings (and from there the "back" navigation button doesn't work at the moment).

Solution 3

This is not currently possible with flutter. The only way is to use platform native code via paltform-channels.

https://flutter.dev/docs/development/platform-integration/platform-channels

Share:
1,704
Eric
Author by

Eric

Updated on December 11, 2022

Comments

  • Eric
    Eric over 1 year

    My app makes local network calls. Is there a way, with flutter/dart, to force http.get() over WiFi (even if internet is not available) rather than 3G/4G?

  • loonix
    loonix almost 4 years
    does not support IOS though
  • Benoit Duffez
    Benoit Duffez almost 3 years
    @DanielC is it required for iOS?