27

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.

1
  • It can be as simple as setting up an app-wide constant like IS_REAL_DEVICE or as complicated as plugins or even a separate main.dart files. Commented May 4, 2021 at 15:48

5 Answers 5

29

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).

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

8 Comments

Still not a reason to do it. You don't want to push dev specific behavior in production.
@RémiRousselet Not entirely sure what you're saying, but I think you're making an assumption here, this is to enable testing/dev to use both physical devices as well as simulator when working.
That's kinda optinion-based so I won't dabate too much. But it's usually better to have configuration files instead of a if (platform == stuff).
There's also the option of wrapping the platform check in an assertion so it only does it in debug mode (as that's probably all you'd use in the simulator anyways). I do see what you mean @Remi, but he's still going to have to have an if (configuration == simulator) or something anyways with a different main, and actually remember that every time he's running on a simulator he has to switch to a different configuration which is a bit of a pain especially if he's on a team with multiple people...
Some of us run multiple devices and simulators at the same time for scaling and design consistency. Certain features like push notifications & video playback, camera aren't supported on the iOS simulator. Helps to have isSimulator
|
21

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){...}
}

1 Comment

The link is dead now
8

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;

2 Comments

Just wanted to mention, that the above answers with device_info_plus are much more viable as it is mainted by the flutter community itself and has a lot more features. but if you aren't interested in other features and solely want to check whether the device is an emulator, my current answer is much more viable.
DO NOT USE safe_device plugin in case you want to check device is simulator / emulator or real device. Because safe_device plugin is increasing app size and causes app freeze issue in release build. Also causing issue with geolocator plugin Check here (1) github.com/ufukhawk/safe_device/issues/42 (2) github.com/ufukhawk/safe_device/issues/33 (3) github.com/Baseflow/flutter-geolocator/issues/1220
0

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

2 Comments

Some of us run multiple devices and simulators at the same time for scaling and design consistency. Certain features like push notifications & video playback, camera aren't supported on the iOS simulator. Helps to have isSimulator
"No", pretty much sounds like "i don't know*". Please do proper research before answering questions. The answer you provided, doesn't align with what the OP has asked for.
0

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

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

3 Comments

Not the greatest solution, since it predates null safety, so the type of isDeviceAnEmulatorOrASimulator is Future<bool*>*
Since this package is discontinued. this answer makes no sense now and should be deleted / removed.
Dart 3 incompatible. discontinued

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.