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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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');

Comments

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.