最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Flutter : check if im running on android ,ios or other in web - Stack Overflow

programmeradmin0浏览0评论

I want to check in Web if I'm running in Android, iOS, or other (desktop).

"Platform.isAndroid" or any other call that uses Platform works only for mobile and desktop apps.

Thanks.

I want to check in Web if I'm running in Android, iOS, or other (desktop).

"Platform.isAndroid" or any other call that uses Platform works only for mobile and desktop apps.

Thanks.

Share Improve this question edited Feb 7 at 7:43 VLAZ 29k9 gold badges62 silver badges83 bronze badges asked Feb 6 at 17:24 Rami DhouibRami Dhouib 351 silver badge6 bronze badges New contributor Rami Dhouib is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 2

You can check the platform in a web environment using JavaScript within Dart (Flutter Web) like this:

import 'dart:html' as html;

String getPlatform() {
  final userAgent = html.window.navigator.userAgent.toLowerCase();

  if (userAgent.contains('android')) {
    return 'Android';
  } else if (userAgent.contains('iphone') || userAgent.contains('ipad') || userAgent.contains('ipod')) {
    return 'iOS';
  } else {
    return 'Other (Desktop)';
  }
}

void main() {
  print(getPlatform()); // Example usage
}

This checks the userAgent string from the browser and determines whether the user is on Android, iOS, or another platform (desktop).

You can use the Flutter Foundation library to detect the current platform as follows:

import 'package:flutter/foundation.dart';

final bool isAndroid = !kIsWeb && defaultTargetPlatform == TargetPlatform.android;  
final bool isIOS = !kIsWeb && defaultTargetPlatform == TargetPlatform.iOS;  
const bool isWeb = kIsWeb;

This works seamlessly across Android, iOS, and Web without any error or warnings.

发布评论

评论列表(0)

  1. 暂无评论