-1.6 C
New York
Thursday, February 2, 2023

Flutter: Detect platform your app is running on

To detect the platform (or OS) your Flutter app is running on, just import dart:io and use the Platform class and its operatingSystem property (String).

Example 1

import 'dart:io';
import 'package:flutter/material.dart';

void main() {
  // print the platform's name to the terminal
  debugPrint(Platform.operatingSystem);
}

Example 2

When running the following code, you’ll see something like the screenshot at the top of this article:Advertisements

import 'package:flutter/material.dart';
// Don't forget this
import 'dart:io';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      // Remove the debug banner
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Kindacode.com'),
      ),
      body: Padding(
        // just add some padding
        padding: const EdgeInsets.all(10),

        // the most important part of this example here
        child: Text(
          'The platform is ${Platform.operatingSystem}',
          style: const TextStyle(fontSize: 30),
        ),
      ),
    );
  }
}

Final Words

You’ve learned how to determine the platform that your Flutter app is running on by using the Platform API from the dart:io package. If you’d like to explore more new and fascinating things about modern Flutter development, take a look at the following articles:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.

Advertisements

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles