1

In an android app, we can use the android.os.Build.VERSION.SDK_INT to get the SDK version of the software currently running on a hardware device. How to do the same in a flutter app so that I can show or hide a widget based on the android build version?

Hasan El-Hefnawy
  • 871
  • 1
  • 11
  • 19

3 Answers3

2

Use device_info plugin to get the SDK version:

var info = await DeviceInfoPlugin().androidInfo;
var sdk = info.version.sdkInt;

And then use it like

Column(
  children: [
    if (sdk > 24) Text('Good'), // Prints only for devices running API > 24
    Text('Morning')
  ],
)

If you don't want to use if you can check this answer for other options.

CopsOnRoad
  • 175,842
  • 51
  • 533
  • 380
1

You can use Device Info Package to Get Device information

https://pub.dev/packages/device_info

You can get all of this information

var androidInfo = await DeviceInfoPlugin().androidInfo;

  var release = androidInfo.version.release;
  var sdkInt = androidInfo.version.sdkInt;
  var manufacturer = androidInfo.manufacturer;
  var model = androidInfo.model;

  print('Android $release (SDK $sdkInt), $manufacturer $model');
0

CopsOnRoad's answer is good, however, when I only need the Build.VERSION.SDK_INT from Android, and I already have MethodChannel implemented for other Android functionality that I need in Flutter, I prefer not to add an extra plugin.

I simply add the following (Java) code:

case METHOD_GET_SDK_INT:
    result.success(Build.VERSION.SDK_INT);
    break;

in the MethodCall handler:

public class MainActivity extends FlutterActivity {
    
    ...

    private static final String METHOD_GET_SDK_INT = "getSdkInt";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
                (call, result) -> {
                    switch (call.method) {
    
                    ...
                    
                    // New code for returning the Android SDK version
                    case METHOD_GET_SDK_INT:
                        result.success(Build.VERSION.SDK_INT);
                        break;

                    ...

Then, in Dart, I use:

final int _androidSdkInt = await platform.invokeMethod('getSdkInt');
Alex Vang
  • 5,261
  • 4
  • 46
  • 59