Flutter - Detect if Application Running on Emulator

This tutorial shows you how to check whether a Flutter application runs on an emulator or a real physical device.

A Flutter application can run on Android and iOS. Since Android and iOS allow applications to be run on an emulator, it also applies for Flutter applications built for those mobile platforms. If you need to know whether the application runs on an emulator or a physical device, you can read the examples in this tutorial. This can be useful if you want to track how many users are using an emulator or for differentiating the behavior if your application runs on an emulator.

Using device_info_plus Package

The first option is using the device_info_plus package. Add the below dependency to your pubspec.yaml file and run flutter pub get.

  device_info_plus: ^8.2.0

First, we need to add the following imports. The device_info_plus package is used to check whether it runs on a physical device if the platform is Android or iOS. The defaultTargetPlatform property of foundation library is used to get the current platform where the application runs because we only need to check for Android and iOS.

  import 'package:device_info_plus/device_info_plus.dart';
  import 'package:flutter/foundation.dart' show defaultTargetPlatform;

First, get the instance of DeviceInfoPlugin by calling the constructor. If the platform is Android, get the AndroidDeviceInfo object from the androidInfo property. Then, access the isPhysicalDevice property of the AndroidDeviceInfo object. If the value is false, it means the application runs on an emulator. It's very similar for iOS. You need to get the IosDeviceInfo object from the iosInfo property. The IosDeviceInfo class has the isPhysicalDevice property as well.

  final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  bool? isEmulator;

  if (defaultTargetPlatform == TargetPlatform.android) {
    final androidInfo = await deviceInfo.androidInfo;
    isEmulator = !androidInfo.isPhysicalDevice;
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    final iosInfo = await deviceInfo.iosInfo;
    isEmulator = !iosInfo.isPhysicalDevice;
  } else {
    isEmulator = false;
  }

Using safe_device Package

Another option is using the safe_device package. Add the below dependency to your pubspec.yaml file and run flutter pub get.

  safe_device: ^1.1.2

You need to add the below import statements. The kIsWeb constant is used to avoid checking if the application runs on the web browser.

  import 'package:flutter/foundation.dart' show kIsWeb;
  import 'package:safe_device/safe_device.dart';

With this package, checking whether the device is a real device or an emulator can be done using SafeDevice.isRealDevice static getter which returns true if it's a real device. To handle that the getter may return an error when executed on a web browser, we need to avoid calling the getter if the platform is web. We can use the kIsWeb constant of the foundation library.

  bool isEmulator = kIsWeb ? false : !await SafeDevice.isRealDevice;

Full Code

  import 'package:device_info_plus/device_info_plus.dart';
  import 'package:flutter/foundation.dart' show defaultTargetPlatform, kIsWeb;
  import 'package:flutter/material.dart';
  import 'package:safe_device/safe_device.dart';
  
  void main() => runApp(const MyApp());
  
  class MyApp extends StatelessWidget {
  
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        title: 'Woolha.com Flutter Tutorial',
        home: CheckIsEmulator(),
      );
    }
  }
  
  class CheckIsEmulator extends StatefulWidget {
  
    const CheckIsEmulator({Key? key}) : super(key: key);
  
    @override
    State<StatefulWidget> createState() {
      return _CheckIsEmulatorState();
    }
  }
  
  class _CheckIsEmulatorState extends State<CheckIsEmulator> {
  
    bool? _isEmulator;
  
    @override
    void initState() {
      super.initState();
      // _checkIsEmulatorUsingPackageInfoPlus();
      _checkIsEmulatorUsingSafeDevice();
    }
  
    void _checkIsEmulatorUsingPackageInfoPlus() async {
      final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      bool? isEmulator;
  
      if (defaultTargetPlatform == TargetPlatform.android) {
        final androidInfo = await deviceInfo.androidInfo;
        isEmulator = !androidInfo.isPhysicalDevice;
      } else if (defaultTargetPlatform == TargetPlatform.iOS) {
        final iosInfo = await deviceInfo.iosInfo;
        isEmulator = !iosInfo.isPhysicalDevice;
      } else {
        isEmulator = false;
      }
  
      setState(() {
        _isEmulator = isEmulator;
      });
    }
  
    void _checkIsEmulatorUsingSafeDevice() async {
      bool isEmulator = kIsWeb ? false : !await SafeDevice.isRealDevice;
  
      setState(() {
        _isEmulator = isEmulator;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Woolha.com Flutter Tutorial'),
          backgroundColor: Colors.teal,
        ),
        body: SizedBox(
          width: double.infinity,
          child: Center(
            child: Text('Is Emulatorerwqe: ${_isEmulator ?? '-'}'),
          ),
        ),
      );
    }
  }

Summary

Checking whether a Flutter application can be done easily by using the help of a package such as device_info_plus or safe_device.

You can also read about: