Flutter: can I find out what platform I am on?

Issue

I am developping a project that should work both as an app and a web page. I am using flutter_secure_storage to store local data for the app, but I need a different approach for web.
Is there a way I can find out what platform I am on at runtime, and choose the correct read-write functions accordingly?

Solution

you can add this extension file to the project and call in any object

target.dart

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

extension Target on Object {
  bool isAndroid() {
    return Platform.isAndroid;
  } 
  bool isIOS() {
    return Platform.isIOS;
  } 
  bool isLinux() {
  return Platform.isLinux;
  } 
  bool isWindows() {
  return Platform.isWindows; 
  }
  bool isMacOS() {
  return Platform.isMacOS; 
  }
  bool isWeb() {
  return kIsWeb; 
  }
  // ยทยทยท
}

and import it anywhere and use it like this

@override
  Widget build(BuildContext context) {
    return isAndroid()? Text("Android"):Text("Not Android");
  }

Answered By – Omar Mahmoud

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *