detectBrowser static method

String detectBrowser(
  1. Request req
)

Detects the browser (using "user-agent") from which the given HTTP request was made.

  • req an HTTP RequestContext request to process. Returns the detected browser. Detectable browsers: "chrome", "msie", "firefox", "safari". Otherwise - "unknown" will be returned.

Implementation

static String detectBrowser(Request req) {
  var ua = req.headersAll['user-agent']?[0] ?? 'unknown';

  if (RegExp(r'chrome').hasMatch(ua)) {
    return 'chrome';
  }
  if (RegExp(r'msie').hasMatch(ua)) {
    return 'msie';
  }
  if (RegExp(r'firefox').hasMatch(ua)) {
    return 'firefox';
  }
  if (RegExp(r'safari').hasMatch(ua)) {
    return 'safari';
  }

  return ua;
}