skip to Main Content

I’ve seen a number of posts asking about detecting the browser and os in Flutter apps which suggest plugins such as client_information and device_info_plus. However, what I haven’t seen is how to detect these things all while running a Flutter web app.

My use-case is that I’d like to prompt the user to add my web app to their home screen. To do that I need to know their OS (don’t want to prompt a macOS user to do this), and browser (Looks like this is a temporary requirement, as currently only safari can install web apps on the home screen so I need to direct them to open my app with Safari, but with iOS 16.4 launching soon third-party browsers will be able to do this as well).

Is there a way to do this?

2

Answers


  1. You can use the defaultTargetPlatform variable from the flutter foundation package.

    import 'package:flutter/foundation.dart';
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: Text(
                "You are running the web application on ${defaultTargetPlatform.name}",
              ),
            ),
          ),
        );
      }
    
    Login or Signup to reply.
  2. You can get the user agent string, which contains information about the operating system and browser:

    import 'dart:html' as html;
    
    void main() {
      String userAgent = html.window.navigator.userAgent;
      print(userAgent);
    }
    

    Output:

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
    

    In addition, you can use user_agent_parser to extract information from the output.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search