🐙

Flutter Package Versionを取得する

2022/05/27に公開

https://pub.dev/packages/package_info_plus

package_info_plusを使えば、appName, packageName, version, buildNumberを取得できる。FutureBuilderを使う必要がある。
以下は例。

class SettingsPage extends ConsumerWidget {
  const SettingsPage({
    Key? key,
  }) : super(key: key);

  static const route = "/settings";

  
  Widget build(BuildContext context, WidgetRef ref) {
  
    return FutureBuilder<PackageInfo>(
      future: PackageInfo.fromPlatform(),
      builder: (BuildContext context, AsyncSnapshot<PackageInfo> snapshot) {
        final packageVersion = (snapshot.hasError || !snapshot.hasData)
            ? ""
            : snapshot.data!.version;

        return Column(
          children: [
            Flexible(
              child: ListView(
                children: [
                  ListTile(
                    leading: const Icon(Icons.info),
                    title: Text(S.of(context).settings_version_title),
                    trailing: Text(packageVersion),
                  )
                ],
              ),
            ),
          ],
        );
      },
    );
  }
}

Discussion