「Flutter/外部ライブラリ/flutter user agent」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==インストール== pubspec.yaml <pre> dependencies: flutter_user_agent: ^1.2.2 </pre> ==公式== https://pub.dev/packages/flutter_user_agent ==サンプル起動=...」) |
(→サンプル起動) |
||
行9: | 行9: | ||
https://pub.dev/packages/flutter_user_agent | https://pub.dev/packages/flutter_user_agent | ||
− | |||
− | |||
==サンプル== | ==サンプル== | ||
<pre> | <pre> |
2020年12月1日 (火) 12:53時点における版
インストール
pubspec.yaml
dependencies: flutter_user_agent: ^1.2.2
公式
https://pub.dev/packages/flutter_user_agent
サンプル
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') ], ), ), ), ); } }