How to get build and version number of Flutter app

后端 未结 4 1468
旧巷少年郎
旧巷少年郎 2020-12-14 05:03

I am currently developing an application which is currently in beta mode. Due to this, I would like to show them what version they are on. For example, \"v1.0b10 - iOS\". So

4条回答
  •  感动是毒
    2020-12-14 05:50

    You can use the get_version to query information about the application Version Name, Version Code, Platform and OS Version, and App ID on iOS and Android

    Add this to your package's pubspec.yaml file:

    dependencies:
      get_version: ^0.2.2
    

    Now in your Dart code, you can use:

    import 'package:get_version/get_version.dart';
    

    Go to build.gradle and update:

    defaultConfig {
      versionCode 1
      versionName "1.0"
      minSdkVersion 16
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    

    How to Use

    class _MyAppState extends State {
      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;
        });
      }
      
    }
    

提交回复
热议问题