## Motivation We'd like to let users opt-in to experimental features so that they can give early feedback while we iterate on the feature. For example: Example feature flags: 1. Android sensitive content: https://github.com/flutter/flutter/pull/158473. When enabled, Flutter will tell Android when the view contains sensitive content like a password. 2. Desktop multi-window. When enabled, Flutter will use child windows to allow things like a context menu to "escape" outside of the current window. ### Use case Users will be able to turn on features by: * **Option 1**: Run `flutter config --enable-my-feature`. This enables the feature for all projects on the machine * **Option 2**: Add `enable-my-feature: true` in their `pubspec.yaml`, under the `flutter` section. This would enable the for a single project on the machine. Turning on a feature affects _both_ development-time (`flutter run`) and deployment-time (`flutter build x`). For example, I can `flutter build windows` to create an `.exe` with multi-window features enabled. ## How this works This adds a new [`runtimeId`](https://github.com/flutter/flutter/pull/168437/files#diff-0ded384225f19a4c34d43c7c11f7cb084ff3db947cfa82d8d52fc94c112bb2a7R243-R247) property to the tool's `Feature` class. If a feature is on and has a `runtimeId`, its `runtimeId` will be [stamped into the Dart application as a Dart define](https://github.com/flutter/flutter/pull/168437/files#diff-bd662448bdc2e6f50e47cd3b20b22b41a828561bce65cb4d54ea4f5011cc604eR293-R327). The framework uses this Dart define to [determine which features are enabled](https://github.com/flutter/flutter/pull/168437/files#diff-c8dbd5cd3103bc5be53c4ac5be8bdb9bf73e10cd5d8e4ac34e737fd1f8602d45). ### Multi-window example https://github.com/flutter/flutter/pull/168697 shows how this new feature flag system can be used to add a multi-window feature flag: 1. It adds a new [multi-window feature](https://github.com/flutter/flutter/pull/168697/files#diff-0ded384225f19a4c34d43c7c11f7cb084ff3db947cfa82d8d52fc94c112bb2a7R189-R198) to the Flutter tool. This can be turned on using `flutter config --enable-multi-window` or by putting `enable-multi-window: true` in an app's .pubspec, under the `flutter` section. 2. It adds a new [`isMultiWindowEnabled`](https://github.com/flutter/flutter/pull/168697/files#diff-c8dbd5cd3103bc5be53c4ac5be8bdb9bf73e10cd5d8e4ac34e737fd1f8602d45R7-R11) property to the framework. 3. The Material library can use this new property to determine whether it should create a new window. [Example](https://github.com/flutter/flutter/pull/168697/files#diff-2cbc1634ed6b61d61dfa090e7bfbbb7c60b74c8abc3a28df6f79eee691fd1b73). ## Limitations ### Tool and framework only For now, these feature flags are available only to the Flutter tool and Flutter framework. The flags are not automatically available to the embedder or the engine. For example, embedders need to configure their surfaces differently if Impeller is enabled. This configuration must happen before the Dart isolate is launched. As a result, the framework's feature flags is not a viable solution for this scenario for now. For these kinds of scenarios, we should continue to use platform-specific configuration like the `AndroidManifest.xml` or `Info.plist` files. This is a fixable limitation, we just need to invest in this plumbing :) ### Tree shaking Feature flags are not designed to help tree shaking. For example, you cannot conditionally import Dart code depending on the enabled feature flags. Code that is feature flagged off will still be imported into user's apps. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
11 KiB
Using feature flags
The Flutter tool (flutter) supports the concept of feature flags, or boolean
flags that can inform, change, allow, or deny access to behavior, either in the
tool itself, or in the framework (package:flutter, and related).
Warning
This document is based on the unmerged PR #168437.
Table of Contents
Overview
For example, enabling the use of Swift Package Manager:
flutter config --enable-swift-package-manager
Feature flags can be configured globally (for an entire machine), locally
(for a particular app), per-test, and be automatically enabled for different
release channels (master, versus beta, versus stable), giving multiple
consistent options for developing.
See also, Flutter pubspec options > Fields > Config.
Why feature flags
Feature flags allow conditionally, consistently, and conveniently changing behavior.
For example:
-
Gradual rollouts to introduce new features to a small subset of users.
-
A/B Testing to easily test or compare different implementations.
-
Kill Switches to quickly disable problematic features without large code changes.
-
Allow experimental access to features not ready for broad or unguarded use.
We do not consider it a breaking change to modify or remove experimental flags across releases, or to make changes guarded by experimental flags. APIs that are guarded by flags are subject to chage at any time.
Adding a flag
Flags are managed in packages/flutter_tools/lib/src/features.dart.
The following steps are required:
-
Add a new top-level
const Feature:const Feature unicornEmojis = Feature( name: 'add unicorn emojis in lots of fun places', );Additional parameters are required to make the flag configurable outside of a unit test.
To allow
flutter config, or inpubspec.yaml'sconfig: ...section, includeconfigSetting:const Feature unicornEmojis = Feature( name: 'add unicorn emojis in lots of fun places', configSetting: 'enable-unicorn-emojis', );To allow usage of the flag in the Flutter framework, include
runtimeId:const Feature unicornEmojis = Feature( name: 'add unicorn emojis in lots of fun places', runtimeId: 'enable-unicorn-emojis', );To allow an environment variable, include
environmentOverride:const Feature unicornEmojis = Feature( name: 'add unicorn emojis in lots of fun places', environmentOverride: 'FLUTTER_UNICORN_EMOJIS', ); -
Add a new field to
abstract class FeatureFlags:abstract class FeatureFlags { /// Whether to add unicorm emojis in lots of fun places. bool get isUnicornEmojisEnabled => false; } -
Implement the same getter in
FlutterFeatureFlagsIsEnabled:mixin FlutterFeatureFlagsIsEnabled implements FeatureFlags { @override bool get isUnicornEmojisEnabled => isEnabled(unicornEmojis); }
Allowing flags to be enabled
By default, after adding a flag, the flag is considered disabled, and cannot be enabled outside of our own unit tests. This allows iterating locally with the code without having to support users or field issues related to the flag.
After some time, you may want to allow the flag to be enabled.
Using the options master, beta or stable, you can make the flag
configurable in those channels. For example, to make the flag available to be
enabled (but still off by default) on the master channel:
const Feature unicornEmojis = Feature(
name: 'add unicorn emojis in lots of fun places',
configSetting: 'enable-unicorn-emojis',
master: FeatureChannelSetting(available: true),
);
Or to make it available on all channels:
const Feature unicornEmojis = Feature(
name: 'add unicorn emojis in lots of fun places',
configSetting: 'enable-unicorn-emojis',
master: FeatureChannelSetting(available: true),
beta: FeatureChannelSetting(available: true),
stable: FeatureChannelSetting(available: true),
);
Enabling a flag by default
Once a flag is ready to be enabled by default, once again it can be configured on a per-channel basis.
For example, enabled on master by default, but disabled by default elsewhere:
const Feature unicornEmojis = Feature(
name: 'add unicorn emojis in lots of fun places',
configSetting: 'enable-unicorn-emojis',
master: FeatureChannelSetting(available: true, enabledByDefault: true),
beta: FeatureChannelSetting(available: true),
stable: FeatureChannelSetting(available: true),
);
Once the flag is ready to be enabled in every environment:
const Feature unicornEmojis = Feature.fullyEnabled(
name: 'add unicorn emojis in lots of fun places',
configSetting: 'enable-unicorn-emojis',
);
Removing a flag
After a flag is no longer useful (perhaps the experiment has concluded, the flag has been enabled successfully for 1+ stable releases), most1 flags should be removed so that the older behavior (or lack of a feature) can be refactored and removed from the codebase, and there is less of a possibility of conflicting flags.
To remove a flag, follow the opposite steps of adding a flag.
You may need to remove references to the (disabled) flag from unit or integration tests as well.
Precedence
Users have several options to configure flags. Assuming the following feature:
const Feature unicornEmojis = Feature(
name: 'add unicorn emojis in lots of fun places',
configSetting: 'enable-unicorn-emojis',
environmentOverride: 'FLUTTER_ENABLE_UNICORN_EMOJIS',
);
Flutter uses the following precendence order:
-
The app's
pubspec.yamlfile:flutter: config: enable-unicorn-emojis: true -
The tool's global configuration:
flutter config --enable-unicorn-emojis -
Environment variables:
FLUTTER_ENABLE_UNICORN_EMOJIS=true flutter some-command
If none of these are set, Flutter falls back to the feature's default value for the current release channel.
Using a flag to drive behavior
Once you have a flag, you can use it to conditionally enable something or provide a different execution branch.
Tool
In the flutter tool, feature flags. flags can be accessed either by adding
(and providing) an explicit FeatureFlags parameter (recommended):
class WebDevices extends PollingDeviceDiscovery {
// While it could be injected from the global scope (see below), this larger
// feature (and tests of it) are made more explicit by directly taking a
// reference to a `FeatureFlags` instance.
WebDevices({required FeatureFlags featureFlags}) : _featureFlags = featureFlags;
final FeatureFlags _featureFlags;
@override
Future<List<Device>> pollingGetDevices({Duration? timeout}) async {
if (!_featureFlags.isWebEnabled) {
return <Device>[];
}
/* ... omitted for brevity ... */
}
}
Or by injecting the currently set flags using the globals pattern:
// Relative path depends on location in the tool.
import '../src/features.dart';
class CreateCommand extends FlutterCommand with CreateBase {
Future<int> _generateMethodChannelPlugin() async {
/* ... omitted for brevity ... */
final List<String> templates = <String>['plugin', 'plugin_shared'];
if ((isIos || isMacos) && featureFlags.isSwiftPackageManagerEnabled) {
templates.add('plugin_swift_package_manager');
}
/* ... omitted for brevity ... */
}
}
Framework
In the framework, feature flags can be accessed by importing
src/foundation/_features.dart:
import 'package:flutter/src/foundation/_features.dart';
final class SensitiveContent extends StatelessWidget {
SensitiveContent() {
if (!debugEnabledFeatureFlags.contains('enable-sensitive-content')) {
throw UnsupportedError('Sensitive content is an experimental feature and not yet available.');
}
}
}
Note that feature flag usage in the framework runtime is very new, and is likely to evolve over time.
Feature flags are not designed to help tree shaking. For example, you cannot conditionally import Dart code depending on the enabled feature flags. Tree shaking might not remove code that is feature flagged off.
Tests
Integration tests
For integration tests representing packages where a flag is enabled, prefer
using the config: property in pubspec.yaml:
flutter:
config:
enable-unicorn-emojis: true
You may see legacy cases where the flag is enabled or disabled globally using
flutter config.
Tool unit tests
For unit tests where the code directly takes a FeatureFlags instance:
final WindowsWorkflow windowsWorkflow = WindowsWorkflow(
platform: windows,
featureFlags: TestFeatureFlags(isWindowsEnabled: true),
);
/* ... omitted for brevity ... */
Or, for larger test suites, or code that uses the global featureFlags getter:
testUsingContext('prints unicorns when enabled', () async {
// You'd write a real test, this is just an example.
expect(featureFlags.isUnicornEmojisEnabled, true);
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isUnicornEmojisEnabled: true),
});
Framework unit tests
Feature flags can be enabled by importing src/foundation/_features.dart:
test('sensitive content should fail if the flag is disabled', () {
final Set<String> originalFeatureFlags = {...debugEnabledFeatureFlags};
addTearDown(() {
debugEnabledFeatureFlags.clear();
debugEnabledFeatureFlags.addAll(originalFeatureFlags);
});
debugEnabledFeatureFlags.remove('enable-sensitive-content');
expect(() => SensitiveContent(), throwsUnsupportedError);
});
Note that feature flag usage in the framework runtime is very new, and is likely to evolve over time.
-
Some flags might have a longer or indefinite lifespan, but this is rare. ↩︎