我使用以下方法获取应用程序名和packageName,但我需要iPhone用户的Bundle id.

 Future<Null> _initPackageInfo() async {
        final PackageInfo info = await PackageInfo.fromPlatform();
        setState(() {
          _packageInfo = info;
          packageName = info.packageName;
          appName = info.appName;
          buildNumber = info.buildNumber;
        });
      }

推荐答案

你可以在iOS和Android上使用get_version 软件包获取应用ID、版本名和版本代码.

将此依赖项添加到你的应用程序,然后像这样获取应用程序id

String projectAppID;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
  projectAppID = await GetVersion.appID;
} on PlatformException {
  projectAppID = 'Failed to get app ID.';
}

完整示例

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_version/get_version.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _projectVersion = '';
  String _projectCode = '';
  String _projectAppID = '';
  String _projectName = '';

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

  // Platform messages are asynchronous, so we initialize in an async method.
  initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await GetVersion.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    String projectVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectVersion = await GetVersion.projectVersion;
    } on PlatformException {
      projectVersion = 'Failed to get project version.';
    }

    String projectCode;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectCode = await GetVersion.projectCode;
    } on PlatformException {
      projectCode = 'Failed to get build number.';
    }

    String projectAppID;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectAppID = await GetVersion.appID;
    } on PlatformException {
      projectAppID = 'Failed to get app ID.';
    }
    
    String projectName;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectName = await GetVersion.appName;
    } on PlatformException {
      projectName = 'Failed to get app 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;
      _projectVersion = projectVersion;
      _projectCode = projectCode;
      _projectAppID = projectAppID;
      _projectName = projectName;
    });
  }

  

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Plugin example app'),
        ),
        body: new SingleChildScrollView(
          child: new ListBody(
            children: <Widget>[
              new Container(
                height: 10.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Name'),
                subtitle: new Text(_projectName),
              ),
              new Container(
                height: 10.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Running on'),
                subtitle: new Text(_platformVersion),
              ),
              new Divider(
                height: 20.0,
              ),
               new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Version Name'),
                subtitle: new Text(_projectVersion),
              ),
              new Divider(
                height: 20.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('Version Code'),
                subtitle: new Text(_projectCode),
              ),
              new Divider(
                height: 20.0,
              ),
              new ListTile(
                leading: new Icon(Icons.info),
                title: const Text('App ID'),
                subtitle: new Text(_projectAppID),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

enter image description here

Flutter相关问答推荐

如何在Flutter 屏幕中添加箭头形状

当仅点击一行时,所有行都被选中

如何解决这个错误,同时获得的高度的小部件,因此高度的可用工作区域在Flutter ?

如何在FiRestore中正确编写OR查询

使用GO_ROUTER在模式内导航

BoxConstraints强制使用无限宽度(列中的Listview)

Flutter 蜂巢不能正常工作,我正在使用蜂巢在设备上存储数据,但当我关闭并重新启动应用程序时,我失go 了一切

升级到Ffltter 3.16后,应用程序栏、背景 colored颜色 、按钮大小和间距都会发生变化

从.gitignore中排除.fvm/fvm_config.json

如何翻转这种剪刀设计

播放特定持续时间Flutter 的音频

为什么我的图标的一半在定位小部件中消失了?

如何使用 Material3 创建带有高度的完美白色提升按钮?

Flutter:如何设置 PersistentFooterButtons 背景?

从所有设备注销,因为用户帐户已从 firebase flutter 中删除

在 CircleAvatar 中放置芯片

无法在Flutter 图像小部件中打开大尺寸(500MB)图像

Flutter RawMaterialButton 常量相对大小

如何在dart中编写多个条件?

如何从用户 Flutter 导航抽屉中隐藏管理菜单