flutter_user_agentx 1.2.3 flutter_user_agentx: ^1.2.3 copied to clipboard
Retrieve device and webview user agent strings for Android and iOS devices. It also provides you with simple building blocks for generating your own user agent string.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_user_agent/flutter_user_agent.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _userAgent = '<unknown>';
String _webUserAgent = '<unknown>';
@override
void initState() {
super.initState();
initUserAgentState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initUserAgentState() async {
String userAgent, webViewUserAgent;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
userAgent = await FlutterUserAgent.getPropertyAsync('userAgent');
await FlutterUserAgent.init();
webViewUserAgent = FlutterUserAgent.webViewUserAgent;
print('''
applicationVersion => ${FlutterUserAgent.getProperty('applicationVersion')}
systemName => ${FlutterUserAgent.getProperty('systemName')}
userAgent => $userAgent
webViewUserAgent => $webViewUserAgent
packageUserAgent => ${FlutterUserAgent.getProperty('packageUserAgent')}
''');
} on PlatformException {
userAgent = webViewUserAgent = '<error>';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_userAgent = userAgent;
_webUserAgent = webViewUserAgent;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('User agent example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Device userAgent: $_userAgent'),
Text('Web userAgent: $_webUserAgent')
],
),
),
),
);
}
}