device_friendly_name 1.1.0 copy "device_friendly_name: ^1.1.0" to clipboard
device_friendly_name: ^1.1.0 copied to clipboard

Retrieve a friendly name for the current device

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:device_friendly_name/device_friendly_name.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _deviceName = 'Unknown';
  final _deviceFriendlyNamePlugin = DeviceFriendlyName();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await _deviceFriendlyNamePlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    String deviceName;
    try {
      deviceName = await _deviceFriendlyNamePlugin.getDeviceFriendlyName() ?? 'Unknown device name';
    } on PlatformException {
      deviceName = 'Failed to get device name';
    }

    // 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(() {
      _platformVersion = platformVersion;
      _deviceName = deviceName;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text('Version: $_platformVersion\n'),
              Text('Name: $_deviceName\n'),
            ],
          ),
        ),
      ),
    );
  }
}
5
likes
140
points
1.88k
downloads

Publisher

verified publishergenzakit.com

Weekly Downloads

Retrieve a friendly name for the current device

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on device_friendly_name