flutter_ua 1.0.0
flutter_ua: ^1.0.0 copied to clipboard
Retrieve Android/iOS device user agents in Flutter.
import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ua/flutter_ua.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
Map<String, dynamic> _properties = {};
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
await FlutterUserAgent.init();
await initPlatformState();
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
var properties = <String, dynamic>{};
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = FlutterUserAgent.userAgent!;
properties = FlutterUserAgent.properties!;
log(platformVersion);
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 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(() {
_properties = properties;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_properties'),
),
),
);
}
}