--- title: How to set up feature flags in Flutter date: 2024-03-05 author: - lior-neu-ner tags: - feature flags --- import { ProductScreenshot } from 'components/ProductScreenshot' export const EventsInPostHogLight = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/flutter-feature-flags/events-light.png" export const EventsInPostHogDark = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/flutter-feature-flags/events-dark.png" export const CreateFlagLight = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/flutter-feature-flags/create-flag-light.png" export const CreateFlagDark = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/flutter-feature-flags/create-flag-dark.png" [Feature flags](/feature-flags) help you conditionally roll out and release features safely. This tutorial shows you how integrate them into your Flutter app using PostHog. We'll create a basic Flutter app, add PostHog, create a feature flag, and then implement the flag to control content in our app. ## 1. Create a new Flutter app Our app will have two screens: 1. The first screen will have a button which takes you to a second screen. 2. The second screen will either have a `red` or `green` background color, depending on whether our feature flag is enabled or not. To set this up, first ensure the [Flutter extension for VS Code](https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter) is installed. Then, create a new app by opening the Command Palette in VS Code (`Ctrl/Cmd + Shift + P`), typing `flutter` and selecting `Flutter: New Project`. Select `Empty Application` and name your app `flutter_feature_flags`. Then, replace your code in `lib/main.dart` with the following: ```dart file=lib/main.dart import 'package:flutter/material.dart'; import 'feature_screen_view.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Flutter Feature Flags App', home: MainScreen(), ); } } class MainScreen extends StatelessWidget { const MainScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Main Screen')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('Hello, world!'), ElevatedButton( child: const Text('Go to Next Screen'), onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => const FeatureScreenView(isFlagEnabled: false)), // We update this later ); }, ), ], ), ), ); } } ``` Lastly, in the `lib` directory, create a new file for our second screen called `feature_screen_view.dart`. Add the following code to it: ```dart import 'package:flutter/material.dart'; class FeatureScreenView extends StatelessWidget { final bool isFlagEnabled; const FeatureScreenView({Key? key, required this.isFlagEnabled}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( backgroundColor: isFlagEnabled ? Colors.green : Colors.red, ); } } ``` Press **F5** and run the app in any emulator (we chose Android) to see your app in action. ![Basic setup of the Flutter app](https://res.cloudinary.com/dmukukwp6/image/upload/v1710055416/posthog.com/contents/images/tutorials/flutter-feature-flags/basic-app.png) ## 2. Add PostHog to your app With our app set up, it’s time to install and set up PostHog. If you don't have a PostHog instance, you can [sign up for free](https://us.posthog.com/signup). To start, install [PostHog’s Flutter SDK](/docs/libraries/flutter) by adding `posthog_flutter` to your `pubspec.yaml`: ```yaml file=pubspec.yaml # rest of your code dependencies: flutter: sdk: flutter posthog_flutter: ^4.0.1 # rest of your code ``` Next, we configure PostHog in each platform using our project API key and instance address. You can find these in [your project settings](https://us.posthog.com/settings/project). ### Android setup For Android, add your PostHog configuration to your `AndroidManifest.xml` file located in the `android/app/src/main`: ```xml file=android/app/src/main/AndroidManifest.xml ``` You'll also need to update the minimum Android SDK version to `21` in `android/app/build.gradle`: ```gradle_kotlin file=android/app/build.gradle // rest of your config defaultConfig { minSdkVersion 21 // rest of your config } // rest of your config ``` ### iOS setup For iOS, you need to have [Cocoapods](https://guides.cocoapods.org/using/getting-started.html) installed. Then add your PostHog configuration to the `Info.plist` file located in the `ios/Runner` directory: ```xml file=ios/Runner/Info.plist com.posthog.posthog.API_KEY com.posthog.posthog.POSTHOG_HOST com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS com.posthog.posthog.DEBUG ``` Then you need to set the minimum platform version to iOS 13.0 in your Podfile: ```yaml file=ios/Podfile platform :ios, '13.0' # rest of your config ``` ### Web setup For Web, add your `Web snippet` (which you can find in [your project settings](https://us.posthog.com/settings/project#snippet)) in the `` of your `web/index.html` file: ```html file=web/index.html ``` ## 3. Create a feature flag in PostHog With PostHog set up, your app is ready for feature flags. To create one, go to the [feature flags tab](https://us.posthog.com/feature_flags) in PostHog and click **New feature flag**. Enter a flag key (like `my-cool-flag`), set the release condition to roll out to 100% of users, and press "Save." You can customize your [release conditions](/docs/feature-flags/creating-feature-flags#release-conditions) with rollout percentages, and [user](/docs/product-analytics/person-properties) or [group properties](/docs/product-analytics/group-analytics) to fit your needs. ## 4. Implement the flag code To implement the feature flag, we: 1. Fetch the `my-cool-flag` flag using [`await Posthog().isFeatureEnabled('my-cool-flag')`](/docs/libraries/flutter#feature-flags). 2. Change the background color of `FeatureScreenView` based on the value of the flag. To do this, update the code in `main.dart` to the following: ```dart filename=main.dart import 'package:flutter/material.dart'; import 'feature_screen_view.dart'; import 'package:posthog_flutter/posthog_flutter.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Flutter Feature Flags App', home: MainScreen(), ); } } class MainScreen extends StatelessWidget { const MainScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Main Screen')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('Hello, world!'), ElevatedButton( child: const Text('Go to Next Screen'), onPressed: () async { bool isFlagEnabled = await Posthog().isFeatureEnabled('my-cool-flag'); if (context.mounted) { Navigator.push( context, MaterialPageRoute(builder: (context) => FeatureScreenView(isFlagEnabled: isFlagEnabled)), ); } } ), ], ), ), ); } } ``` That's it! When you restart your app and click the button, you should see the green background color on the second screen. ## Further reading - [A software engineer's guide to A/B testing](/product-engineers/ab-testing-guide-for-engineers) - [How to run A/B tests in Flutter](/tutorials/flutter-ab-tests) - [How to set up analytics in Flutter](/tutorials/flutter-analytics) - [How to set up remote config in Flutter](/tutorials/flutter-remote-config)