Kill Switch — Demo Implementation in Flutter

What if you could undo on the fly?

Chahat Gupta
5 min readSep 2, 2023

Out of all features in Firebase, I love Remote Config the most. This might be the most merciful offer Google has made to developers. If you don’t know about it: it’s just a JSON that gets downloaded (ideally) at the app launch and can contain small chunks of text data that you may change on the fly. This data is also customizable across users.

I have used it for many good and bad things, but last year my team made the most out of it by making a kill-switch mechanism. The idea is simple: store a bunch of booleans with Firebase and use it as an entry check for new or risky features. Let’s understand it this way:

You wish to have control over your app such that:

  1. If something breaks on production (either mobile side or backend) you don’t want users to know about the feature at all until it’s fixed.
  2. You want to roll out the feature for a specific group of users only.
  3. You don’t want to uncover the feature to all segments of users at once, but gradually.

Kill switch will make it possible for you. Let’s see the implementation:

Step #1 — Set up a project on Firebase and connect your app

If not already, connect your app to Firebase. It takes less than 10 minutes. You can find the detailed guide on this page

Step #2 — Create a Remote Config

Create a remote config for your project if it’s not already active. You can find the option on the sidebar under the Release & Monitor category.

Step #3 — Start adding values

Add one or more key-value pairs to the remote config. I like to add a common prefix to all kill switch keys but you can name these whatever you want. Add a key name (i have used switch_feature_community), choose the data type Boolean, add a default value (true or false) and save.

You may add more of these gradually. I am demonstrating with three switches. Add your keys and click Publish Changes. Your dashboard may look something like this.

Step #4 — Let’s code!

First, we create our KillSwitch class which will manage all the ins and outs for us.

//  kill_switch.dart

import 'package:firebase_remote_config/firebase_remote_config.dart';

// all the keys that we added at Firebase Console
abstract class FeatureKey {
static const String announcements = 'switch_feature_announcements';
static const String chat = 'switch_feature_chat';
static const String community = 'switch_feature_community';
}

class KillSwitch {
static final FirebaseRemoteConfig _remoteConfig = FirebaseRemoteConfig.instance;

// initializing Remote Config
static Future<void> initialize() async {
await _remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 10),
minimumFetchInterval: Duration.zero),
);
await _remoteConfig.fetchAndActivate();
}

// our friendly function
static bool hasEnabled(String featureKey) {
return _remoteConfig.getBool(featureKey);
}
}

Next, initialize Firebase and our KillSwitch once at app start, ideally in the main() function.

//  main.dart

import 'package:firebase_core/firebase_core.dart'
import 'package:hub/kill_switch.dart';

void main() async {

// call only once in main.dart
await Firebase.initializeApp();
await KillSwitch.initialize();
}

Believe it or not, but this is all the setup we needed. We can now call the hasEnabled() function from anywhere in code: be it UI or logic. Let’s see an example use case:

// chat_view.dart

import 'package:flutter/material.dart';
import 'package:hub/kill_switch.dart';

class ChatView extends StatelessWidget {
@override
Widget build(BuildContext context) => Scaffold(
// using our friendly function to access the
// boolean in the UI, you may want to get this
// from a controller depending on your architecture.
floatingActionButton: KillSwitch.hasEnabled(FeatureKey.chat)
? FloatingActionButton(
onPressed: () {
...
},
child: Icon(Icons.chat))
: null,
);
}

In the above example, the FloatingActionButton will only be visible on the screen if we have our switch_feature_chat set to true on our Firebase Console.

Step #5 (Optional) — Enabling a feature based on user segment

You will enjoy Firebase to the fullest if you use its multiple features side by side. To achieve kill switch based on user segment, you will first need to have a user segment. This blog is less about analytics so I will tell you what to do and leave the correct links.

1. Set up Firebase Analytics and set user properties.
https://pub.dev/packages/firebase_analytics

2. Create conditions on Firebase based on these properties.

3. Add conditional values based on these properties.

Congratulations! You just enabled a feature for a specific user segment. You can modify this on the fly and keep enabling or disabling features based on your strategy.

Conclusion

This was a promised blog, and as simple as it may look, this small, one-time implementation can give your app audience a highly customised experience. These tiny things only add up to a great user-facing application. Let me know what you think and let’s meet in another blog. Happy coding!

🚀 Let’s connect!

Connect with me on LinkedIn for professional insights and networking.

Explore my contributions on GitHub and Stack Overflow to collaborate on something amazing! ✨

--

--

Chahat Gupta

Mobile Tech Lead specialising in Android, iOS, and Flutter. Sharing insights and learnings on mobile development to inspire and elevate tech professionals.