Skip to content

Retrieve the current Dart and Flutter SDK semantic versions with easy-to-use nullable getters for streamlined version management.

License

Notifications You must be signed in to change notification settings

pro100svitlo/dart_flutter_version

Repository files navigation

dart_flutter_version

pub.dev Check for updates Bintray License

Retrieve the current Flutter and Dart SDK version at runtime with easy-to-use nullable getters for seamless and dynamic version management.

Content

Why Use dart_flutter_version?

Flutter does not provide a built-in way to determine the Flutter SDK version during runtime. This limitation can pose challenges when you need to:

  • Detect Flutter Version Programmatically: Allow your application/package to identify the specific Flutter version it was compiled with, enabling conditional logic based on version-specific features or behaviors.
  • Ensure Compatibility: Verify compatibility with specific Flutter features or APIs that may vary between versions.
  • Debug and Log Version Information: Log or display version information for debugging purposes, helping to diagnose issues related to SDK versions.
  • Manage Dependencies: Handle dependencies or conditional logic based on the Flutter SDK version to maintain stability and functionality across different environments.

Prerequisites

  • Dart SDK: >=2.17.0
  • Flutter SDK: >=3.0.0

Features

  • Dart Version Retrieval: Easily obtain the current Dart SDK version in a simplified MAJOR.MINOR.PATCH format.
  • Flutter Version Resolution: Automatically resolve the corresponding Flutter SDK version based on the current Dart version.
  • Runtime Availability: Access version information during runtime, which is not natively supported by Flutter.
  • Extensible Mapping: Maintain an up-to-date mapping between Dart and Flutter versions with an autogenerated lookup table.
  • Lightweight and Easy to Use: Minimal dependencies and simple API for seamless integration into your Flutter projects.

How It Works

Dart Version Retrieval

The package parses the Dart SDK version from the Platform.version string, extracting the MAJOR.MINOR.PATCH components. This is handled by the DartVersionParser class, which uses a regular expression to ensure accurate parsing.

Flutter Version Resolution

Since Flutter SDK versions are closely tied to specific Dart SDK versions, the package includes a mapping (dartToFlutterMap) that associates Dart versions with their corresponding Flutter versions. This mapping is generated by analyzing the stable channel release archives from Flutter's official documentation.

dartToFlutterMap Crafting

The dartToFlutterMap is an autogenerated map that links Dart SDK versions to their corresponding Flutter SDK versions. It is derived from the stable channel archives on macOS (official Flutter documentation). Key highlights:

  • Reliable Data: Based on official stable releases.
  • Autogenerated: Automatically updated to reflect the latest versions, reducing manual effort.
  • Starts at Flutter 3.0.0: Covers versions from Flutter 3.0.0 (Dart 2.17.0) onward.
  • One-to-One Mapping: Ensures each Flutter version is uniquely associated with a Dart version.

To learn how the map is generated, check the update_files.dart script.

Accuracy Considerations

While the dartToFlutterMap provides a reliable association between Dart and Flutter versions, there are some limitations:

  • Stable Channel Only: The mapping includes only stable channel releases. Other channels like beta, dev, or master may have different version alignments not reflected in the map.
  • Platform Variations: The map is based on macOS stable releases. Version alignments on other platforms (Windows, Linux) might differ.
  • Release Timing: Newly released versions may not be immediately reflected in the map, potentially causing slight delays in accuracy.
  • Minimum Flutter Version: The resolver only supports Flutter SDK versions 3.0.0 and above. Flutter versions below 3.0.0 are not recognized, limiting the ability to detect older Flutter SDK versions.

As a result, while the resolved Flutter version is generally accurate, it may NOT be 100% precise in all scenarios.

How to Use

Integrate the Package

Add dart_flutter_version to your project's pubspec.yaml dependencies:

dependencies:
  dart_flutter_version: 

Then, fetch the package by running:

$ flutter pub get

Import the Package

In your Dart file, import the dart_flutter_version package:

import 'package:dart_flutter_version/dart_flutter_version.dart';

Example Usage

Below is a simple example demonstrating how to use the dart_flutter_version package to print the current Dart and Flutter SDK versions and execute conditional logic based on the Flutter version.

import 'package:flutter/material.dart';
import 'package:dart_flutter_version/dart_flutter_version.dart';
import 'package:pub_semver/pub_semver.dart';

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

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  /// Create an instance of the [DartFlutterVersion] class
  final DartFlutterVersion versionInfo = DartFlutterVersion();

  @override
  Widget build(BuildContext context) {
    final Version? dartVersion = versionInfo.dartVersion;
    final Version? flutterVersion = versionInfo.flutterVersion;

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Version Info')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Display the Dart version if known
              Text('Dart SDK Version: ${dartVersion ?? "Unknown"}'),

              // Display the Flutter version if known
              Text('Flutter SDK Version: ${flutterVersion ?? "Unknown"}'),

              ElevatedButton(
                onPressed: () {
                  // Callback to track current Dart and Flutter versions
                  print('Dart SDK Version: ${dartVersion ?? "Unknown"}');
                  print('Flutter SDK Version: ${flutterVersion ?? "Unknown"}');
                },
                child: const Text('Print SDK Versions'),
              ),

              ElevatedButton(
                onPressed: () {
                  // Method to check Flutter version and execute conditional logic
                  if (flutterVersion != null && flutterVersion >= Version(3, 21, 0)) {
                    print('Running on Flutter 3.21.0 or newer');
                  } else {
                    print('Running on an older Flutter version');
                  }
                },
                child: const Text('Check Flutter Version'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Contributing

Contributions are welcome! To contribute:

  1. Go to GitHub: Visit the dart_flutter_version repository.

  2. Open a Pull Request (PR): Fork the repository and create a PR with your changes.

  3. Provide a Clear Description: Clearly describe what your PR does and why.

  4. Provide Examples: Include examples to demonstrate your changes.

About

Retrieve the current Dart and Flutter SDK semantic versions with easy-to-use nullable getters for streamlined version management.

Topics

Resources

License

Stars

Watchers

Forks

Languages