UA Parser Dart

pub package GitHub license GitHub stars

A comprehensive Dart library for parsing User-Agent strings to extract browser, operating system, device, engine, and CPU information.

This library is a Dart port of the popular UAParser.js library, providing the same functionality with idiomatic Dart code and a clean, modular architecture.

Features

  • 🌐 Browser, OS, Device Detection: Extract comprehensive information from User-Agent strings
  • ⚙️ Engine & CPU Architecture: Identify rendering engines and CPU architectures

Getting started

Add this package to your pubspec.yaml:

dependencies:
  ua_parser: ^1.0.0

Then import it in your Dart code:

import 'package:ua_parser/ua_parser.dart';

Usage

Quick Start

import 'package:ua_parser/ua_parser.dart';

void main() {
  // Parse a user agent string directly
  final userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1';
  
  final result = UaParser.parse(userAgent);
  
  print('Browser: ${result.browser.name} ${result.browser.version}');
  print('OS: ${result.os.name} ${result.os.version}');
  print('Device: ${result.device.vendor} ${result.device.model}');
  print('Engine: ${result.engine.name} ${result.engine.version}');
  print('CPU: ${result.cpu.architecture}');
}

Using Parser Instance

// Create a parser instance for multiple operations
final parser = UaParser();

// Set user agent
parser.setUserAgent(userAgent);

// Get individual components
final browser = parser.getBrowser();
final os = parser.getOs();
final device = parser.getDevice();
final engine = parser.getEngine();
final cpu = parser.getCpu();

// Get complete result
final result = parser.getResult();

// Convert to Map for JSON serialization
final map = result.toMap();

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Ported from UAParser.js v1.0.x by Faisal Salman
  • Inspired by the need for a comprehensive UA parser in Dart/Flutter ecosystem

Libraries

ua_parser