Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get device identifiers in Flutter

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to obtain device identifiers in Flutter related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this article on how to obtain device identifiers in Flutter will have a harvest, let's take a look at it.

Use platform_device_id

If you only need the id of the device running the application, the easiest and quickest solution is to use the platform_device_id package. It works with Android (AndroidId), iOS (IdentifierForVendor), Windows (BIOS UUID), macOS (IOPlatformUUID), and Linux (BIOS UUID). In the Flutter Web application, you will get UserAgent (this information is not unique).

Apply Preview

The sample application we are going to build contains a floating button. When this button is pressed, the ID of the device is displayed on the screen. Here's how it works on iOS and Android:

Code

1. Install the plug-in by running:

Flutter pub add platform_device_id

Then execute this command:

Flutter pub get

No special permissions or configurations are required.

two。 Complete code:

/ / main.dartimport 'package:flutter/material.dart';import' package:platform_device_id/platform_device_id.dart';void main () {runApp (const MyApp ());} class MyApp extends StatelessWidget {const MyApp ({Key? Key}): super (key: key); @ override Widget build (BuildContext context) {return MaterialApp (/ / Remove the debug banner debugShowCheckedModeBanner: false, title: 'Journey to the Big Front end', theme: ThemeData (primarySwatch: Colors.indigo,), home: const HomePage ());} class HomePage extends StatefulWidget {const HomePage ({Key? Key}): super (key: key); @ override _ HomePageState createState () = > _ HomePageState ();} class _ HomePageState extends State {String? _ id; / / This function will be called when the floating button is pressed void _ getInfo () async {/ / Get device id String? Result = await PlatformDeviceId.getDeviceId; / / Update the UI setState (() {_ id = result;});} @ override Widget build (BuildContext context) {return Scaffold (appBar: AppBar (title: const Text)), body: Padding (padding: const EdgeInsets.all (20), child: Center (child: Text (_ id??) 'Press the button', style: TextStyle (fontSize: 20, color: Colors.red.shade900), floatingActionButton: FloatingActionButton (onPressed: _ getInfo, child: const Icon (Icons.play_arrow)),);}} use device_info_plus

The package device_info_plus provides you with the device ID as platform_device_id and other detailed information about the device (brand, model, etc.) and the Android or iOS version of the Flutter application running.

Apply Preview

The application we will make is very similar to the one in the previous example. However, this time we will display a lot of text on the screen. The returned results vary from platform to platform. As you can see, the amount of information returned on Android far exceeds that of iOS.

Code

1. Install the plug-in by doing the following:

Flutter pub add device_info_plus

Then run:

Flutter pub get

2. Complete source code in main.dart:

/ / main.dartimport 'package:flutter/material.dart';import' package:device_info_plus/device_info_plus.dart';void main () {runApp (const MyApp ());} class MyApp extends StatelessWidget {const MyApp ({Key? Key}): super (key: key); @ override Widget build (BuildContext context) {return MaterialApp (/ / Remove the debug banner debugShowCheckedModeBanner: false, title: 'Journey to the Big Front end', theme: ThemeData (primarySwatch: Colors.amber,), home: const HomePage ());} class HomePage extends StatefulWidget {const HomePage ({Key? Key}): super (key: key); @ override _ HomePageState createState () = > _ HomePageState ();} class _ HomePageState extends State {Map? _ info; / / This function is triggered when the floating button gets pressed void _ getInfo () async {/ / Instantiating the plugin final deviceInfoPlugin = DeviceInfoPlugin (); final result = await deviceInfoPlugin.deviceInfo; setState (() {_ info = result.toMap ();}) } @ override Widget build (BuildContext context) {return Scaffold (appBar: AppBar (title: const Text)), body: _ info! = null? Padding (padding: const EdgeInsets.all (20), child: ListView (children: _ infostructures. Room.map ((e) = > Wrap (children: [Text ("${e.key}:") Style: const TextStyle (fontSize: 18, color: Colors.red),), const SizedBox (width: 15,) Text (e.value.toString (), style: const TextStyle (fontSize: 18,))] ) .toList (),): const Center (child: Text ('Press the button'),), floatingActionButton: FloatingActionButton (onPressed: _ getInfo, child: const Icon (Icons.info),),) }} this is the end of the article on "how to get device identifiers in Flutter". Thank you for reading! I believe you all have a certain understanding of "how to obtain device identifiers in Flutter". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report