user_agent
A pure Dart user-agent string parser with browser, OS, device type, and bot detection.
Features
- Pure Dart — no native dependencies
- Browser detection with version extraction
- Operating system detection with version parsing
- Device type classification (desktop, mobile, tablet, bot)
- Extensive bot detection (search engines, AI crawlers, testing frameworks, HTTP clients)
Installation
dependencies:
user_agent: ^1.0.0Usage
Parsing a User-Agent String
import 'package:user_agent/user_agent.dart';
final ua = UserAgent.parse(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ...',
);
print(ua.browser); // Browser.chrome
print(ua.browserVersion); // "120.0.0.0"
print(ua.os); // OperatingSystem.macOS
print(ua.osVersion); // "10.15.7"
print(ua.deviceType); // DeviceType.desktopConvenience Checks
if (ua.isBot) {
return Response.forbidden('Bots not allowed');
}
if (ua.isMobile) {
// Serve mobile layout
}
if (ua.isDesktop) {
// Serve desktop layout
}With Shelf Requests
Response handleRequest(Request request) {
final ua = UserAgent.parse(
request.headers['user-agent'] ?? '',
);
print('${ua.browser.displayName} on ${ua.os.displayName}');
return Response.ok('Hello');
}Bot Detection
The parser detects a wide range of bots:
- Search engines — Googlebot, Bingbot, DuckDuckBot, YandexBot
- Social media — facebookexternalhit, Twitterbot, LinkedInBot
- AI crawlers — GPTBot, ClaudeBot, PerplexityBot
- Testing frameworks — Puppeteer, Selenium, Playwright
- HTTP clients — curl, wget, python-requests, axios
Enums
Browser — chrome, firefox, safari, edge, opera, samsungInternet, ie, vivaldi, brave, ucBrowser, yandex, wechat, twitter, unknown
OperatingSystem — windows, macOS, linux, android, iOS, chromeOS, windowsPhone, unknown
DeviceType — desktop, mobile, tablet, bot, unknown
All enums have a displayName property for human-readable output.