본문 바로가기
Flutter/Flutter FAQ

flutter 다트 코드에서 호스트 플랫폼을 어떻게 감지하나요?, How do you detect the host platform from Dart code?

by 베타코드 2023. 5. 8.
반응형

질문


For UI that should differ slightly on iOS and Android, i.e. on different platforms, there must be a way to detect which one the app is running on, but I couldn't find it in the docs. What is it?

UI가 iOSAndroid에서 약간 다른 경우, 즉 다른 플랫폼에서는 응용 프로그램이 실행 중인 플랫폼을 감지하는 방법이 있어야하지만 문서에서 찾을 수 없었습니다. 그것은 무엇입니까?


답변


import 'dart:io' show Platform;

if (Platform.isAndroid) {
  // Android-specific code
} else if (Platform.isIOS) {
  // iOS-specific code
}

All options include:

Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows

You can also detect if you are running on the web using kIsWeb, a global constant indicating if the application was compiled to run on the web:

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

if (kIsWeb) {
  // running on the web!
} else {
  // NOT running on the web! You can check for additional platforms here.
}
반응형

댓글