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.2 Answers
Reset to default 2You 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.