dart_flutter_version 0.1.2
dart_flutter_version: ^0.1.2 copied to clipboard
Retrieve the current Dart and Flutter SDK semantic versions with easy-to-use nullable getters for streamlined version management.
dart_flutter_version #
Retrieve the current Dart and Flutter SDK semantic versions with easy-to-use nullable getters for streamlined 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 lookup table that matches Dart SDK versions to their corresponding Flutter SDK versions. Key points about the map:
- Source of Data: The map is created by examining stable channel releases on macOS, ensuring reliability based on official releases.
- Autogenerated Content: The map is generated automatically to reflect the latest stable releases, eliminating manual maintenance and potential errors.
- Starting Point: The mapping begins from Flutter version
3.0.0
, which aligns with Dart version2.17.0
. - Uniqueness: Each Flutter version appears only once, providing a one-to-one relationship for straightforward lookups.
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:
-
Go to GitHub: Visit the dart_flutter_version repository.
-
Open a Pull Request (PR): Fork the repository and create a PR with your changes.
-
Provide a Clear Description: Clearly describe what your PR does and why.
-
Provide Examples: Include examples to demonstrate your changes.