25

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.
    – sjsam
    Commented May 4, 2021 at 15:48

5 Answers 5

27

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

8
  • Still not a reason to do it. You don't want to push dev specific behavior in production. Commented Apr 17, 2018 at 8:35
  • 1
    @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. Commented Apr 17, 2018 at 14:10
  • 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). Commented Apr 17, 2018 at 14:36
  • 2
    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... Commented Apr 17, 2018 at 16:22
  • 2
    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 Commented Feb 29, 2020 at 11:24
19

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

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
1

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

import 'package:flutter_is_emulator/flutter_is_emulator.dart';
....
bool isAnEmulator = await FlutterIsEmulator.isDeviceAnEmulatorOrASimulator;
2
  • Not the greatest solution, since it predates null safety, so the type of isDeviceAnEmulatorOrASimulator is Future<bool*>* Commented Apr 22, 2023 at 6:07
  • Since this package is discontinued. this answer makes no sense now and should be deleted / removed. Commented Aug 26, 2023 at 9:35
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
  • 1
    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 Commented Feb 29, 2020 at 11:24
  • "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. Commented Aug 26, 2023 at 9:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.