'How to determine if flutter is running in simulator

Is there a call to determine if flutter is running within a simulator or a physical device?

I am scanning QR codes, and want to bypass, since the camera is unavailable.

I expected to find this in platform.dart[1] but it's not there.

[1]https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/platform.dart

I imagine I can create a plugin if I really need, I'm hoping it already exists.



Solution 1:[1]

Using the device info plus plugin you can get various information about the device you're running on, including 'isPhysicalDevice' for both Android and iOS (although you'll have to read them independently).

Solution 2:[2]

2021 Update

It‘s now part of Flutter Community Plus (https://plus.fluttercommunity.dev/)

Device Info Plus Docu: https://plus.fluttercommunity.dev/docs/device_info_plus/overview

e.g.:

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if(Platform.isIOS){
  var iosInfo = await deviceInfo.iosInfo;
  if(iosInfo.isPhysicalDevice){...}
}

Solution 3:[3]

No. But what you can do instead is use different configurations (such as a dev configuration).

For this you can use a different main.dart such as main.dev.dart and then run it with flutter run -t lib/main.dev.dart

Solution 4:[4]

I'm using https://pub.dev/packages/flutter_is_emulator

import 'package:flutter_is_emulator/flutter_is_emulator.dart';
....
bool isAnEmulator = await FlutterIsEmulator.isDeviceAnEmulatorOrASimulator;

Solution 5:[5]

I Know I'm A Bit Late, But If Anyone Else Comes Here, This Can Help Them. You Can Just Use This Package: https://pub.dev/packages/safe_device

Add The Latest Version In Your Pubspec.yaml File Then import it:

import 'package:safe_device/safe_device.dart';

Then You Can Check If Device Is An Emulator:

bool isRealDevice = await SafeDevice.isRealDevice;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ahmed Ashour
Solution 3 Rémi Rousselet
Solution 4 Ricardo
Solution 5 Dharman