Ariel Ramos
 · Owner Codingraph

Get current App version

Have you ever wonder how to automatically get the app version in FF? Here you have a widget that I did: parameters, code and screenshots, it  returns the version number, so you can customize it. No more writting the version number manually inside the app. PD: Also you can get Build number, Package name, Installer Store and Build Signature, this info is declared in: class _CurrentAppVersionState SCREENSHOTS ====================================== This is how it looks like inside FF Builder[Screen Shot 2022-10-24 at 10.27.33.png]And this is how it looks when you run it [Screen Shot 2022-10-24 at 10.43.07.png] ====================================== FF SCREENSHOTS [Screen Shot 2022-10-24 at 10.27.38.png][Screen Shot 2022-10-24 at 10.27.47.png] Look that Custom Widget windows will show errors but remember that FF does not compile instantly when you are coding: [Screen Shot 2022-10-24 at 10.27.55.png]====================================== DEPENDENCIES: package_info_plus: ^3.0.1 package_info_plus_platform_interface: ^2.0.1 ====================================== CODE:


import 'package:package_info_plus/package_info_plus.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:package_info_plus_platform_interface/method_channel_package_info.dart';
import 'package:package_info_plus_platform_interface/package_info_data.dart';
import 'package:package_info_plus_platform_interface/package_info_platform_interface.dart';


class CurrentAppVersion extends StatefulWidget {
  const CurrentAppVersion({
    Key? key,
    this.width,
    this.height,
    required this.myText,
  }) : super(key: key);

  final double? width;
  final double? height;
  final String myText;

  @override
  _CurrentAppVersionState createState() => _CurrentAppVersionState();
}

class _CurrentAppVersionState extends State {
  PackageInfo _packageInfo = PackageInfo(
    appName: 'Unknown',
    packageName: 'Unknown',
    version: 'Unknown',
    buildNumber: 'Unknown',
    buildSignature: 'Unknown',
    installerStore: 'Unknown',
  );

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

  Future _initPackageInfo() async {
    final info = await PackageInfo.fromPlatform();
    setState(() {
      _packageInfo = info;
    });
  }

  Widget _infoTile(String title, String subtitle) {
    return ListTile(
      title: Text(title),
      subtitle: Text(subtitle.isEmpty ? 'Not set' : subtitle),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _infoTile(widget.myText, _packageInfo.version),
        ],
      ),
    );
  }
}
4
10 replies