6

I want to use different apps on PC and mobile, so what would be the best way to find out what the device is before loading the app?

Shambhav
  • 73
  • 1
  • 4
  • 1
    Does this answer your question? [How do you detect the host platform from Dart code?](https://stackoverflow.com/questions/45924474/how-do-you-detect-the-host-platform-from-dart-code) – Mobina Aug 15 '20 at 16:12

3 Answers3

9

As dart:io is not supported on the web, another solution is to look at the user agent (warning: a user could fake this to load the mobile or desktop version anyway).

import 'package:universal_html/html.dart' as html;

const appleType = "apple";
const androidType = "android";
const desktopType = "desktop";

String getSmartPhoneOrTablet() {
  final userAgent = html.window.navigator.userAgent.toString().toLowerCase();
  // smartphone
  if( userAgent.contains("iphone"))  return appleType;
  if( userAgent.contains("android"))  return androidType;

  // tablet
  if( userAgent.contains("ipad")) return appleType;
  if( html.window.navigator.platform.toLowerCase().contains("macintel") && html.window.navigator.maxTouchPoints > 0 ) return appleType;

  return desktopType;
}

Credit to: https://github.com/flutter/flutter/issues/41311#issuecomment-739854345

Gpack
  • 1,878
  • 3
  • 18
  • 45
  • Please accept this answer instead, as `Platform` does not work in web apps! /// I think there is no problem with looking at the user agent, provided that you only rely on it for adapting the visual UI. If the user changes their user agent string, I think it's safe to assume that they would prefer (or at least expect) the app variant tailored to that user agent. – Arthur Khazbs Jul 22 '22 at 13:32
1

You can easily findout with this code

bool isMobile = MediaQuery.of(context).size.width < 850;
bool isTablet = MediaQuery.of(context).size.width < 1100 &&
    MediaQuery.of(context).size.width >= 850;
bool isDesktop = MediaQuery.of(context).size.width >= 1100;
Anand
  • 4,355
  • 2
  • 35
  • 45
0

You can do this with the Platform class of of dart:io, which provides a few static properties that can help you determine the OS and therefore PC vs mobile.

The following return bools that help you determine each OS

isAndroid, isFuchsia, isIOS, isLinux, isMacOS, isWindows

Or you can use the operatingSystem property, which returns a String containing the OS.

Ex.

if(Platform.isWindows)
...

In general, if you see Android & iOS as the OS, you'll know it's mobile. If you see Linux, MacOS, or Windows, you'll know it's PC. And Fuchsia is a bit ambiguous.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • 3
    On the web this does not work for me. I get the following error `Unsupported operation: Platform._operatingSystem` – Daniel S. Apr 23 '21 at 07:37
  • 3
    @DanielS. You cannot use `dart:io` in web libraries. You should be getting a linter warning about this. If you want to find this information in the web, you'll need to use a separate package or make it yourself. – Christopher Moore Apr 23 '21 at 12:37