Remote Config without foot-guns: clamping every knob in a Flutter app

2026-11-30来源 dev.toEnglish

I run a small Android app that finds duplicate and similar photos and moves them to the system Trash. It has a free monthly allowance, a Pro one-time purchase, and a few ads. Almost every number that touches money or annoyance is a Firebase Remote Config key, because I want to tune it without shipping a build.

That is also the scary part. Remote Config is a text box in a web console. One typo, one extra zero, and every user gets a 0 MB allowance or an ad after every tap. Nobody reviews that change. It ships in minutes.

So every knob in the app goes through the same rule: read, parse, clamp, fall back. This post is that rule, and the three places it saved me from myself.

1. One clamp function, used everywhere

static int _clamp(Object? v, int fallback, int lo, int hi) { int? n; if (v is int) n = v; if (v is double && v.isFinite && v == v.roundToDouble()) n = v.toInt(); if (v is String) n = int.tryParse(v.trim()); if (n == null) return fallback; return n < lo ? lo : (n > hi ? hi : n); } 

Three things matter here:

  • Garbage returns the code default, not 0. An empty string, "300MB", NaN, a JSON object: all become the default that shipped in the app.
  • Out of range is pulled back in, not rejected. 5000 becomes the ceiling. I would rather run at the edge of a safe range than silently fall back and not notice.
  • The range lives next to the key, in code, reviewed in a pull request. The console can move a value inside the box. It cannot move the box.

2. The free allowance

The free tier has three knobs:

factory FreeTierKnobs.fromValues(Map<String, Object?> v) { const d = FreeTierKnobs(); // 300 MB, +300 MB per rewarded ad, 3 per month return FreeTierKnobs( freeMonthlyMb: _clamp(v['pc_free_monthly_mb'], d.freeMonthlyMb, 100, 2000), rewardBonusMb: _clamp(v['pc_reward_bonus_mb'], d.rewardBonusMb, 100, 1000), maxRewardsPerMonth: _clamp(v['pc_max_rewards_per_month'], d.maxRewardsPerMonth, 0, 6), ); } 

The floor of 100 MB is a product decision written as code: the free tier must always let you finish at least a small clean. The ceiling of 2000 MB stops me from accidentally giving the paid feature away.

The values are cached as JSON in SharedPreferences and parsed through the same fromValues, so a corrupted cache also lands on the defaults instead of crashing the app on launch.

Changing the deal for existing users. In this version the free allowance went down. Dropping someone from "plenty left" to "0 left" in the middle of a month, right after an update, is the fastest way to earn a one-star review. So the first launch of the new build decides, once, whether this install existed before: it looks for preference keys that only older versions wrote. If it did, that install keeps the old, larger terms until the end of the current calendar month, and gets the new ones from the next month on. New installs get the new terms immediately. The check has to run before anything else writes preferences in that launch, otherwise every install looks "old".

3. Ads: knobs on top of floors

The interstitial added in this version only plays after you tap Done on the "cleaned" screen. Not on Back, not when you open the Trash, never on its own. Remote Config can tune when it starts:

rolloutPct: _clamp(v['pc_interstitial_rollout_pct'], d.rolloutPct, 0, 100), minPriorCleans: _clamp(v['pc_interstitial_min_prior_cleans'], d.minPriorCleans, 2, 20), minHours: _clamp(v['pc_interstitial_min_hours'], d.minHours, 24, 168), 

Notice the lower bounds. Even if I set "0 prior cleans, 0 hours", the app still waits for at least 2 previous cleans and 24 hours after install. Under those knobs there are floors that Remote Config does not touch at all: at most one per session, one per day, 24 hours between them, and never for anyone who bought Pro or Remove Ads. The console can make ads rarer. It cannot make them more frequent than the code allows.

4. One thing per screen

The "cleaned" screen is also where the app may ask for a Play review or show a quiet Pro line. Three asks on one screen is how you get uninstalled, so the screen gets exactly one slot, picked in a fixed order:

if (review) return DoneSlot.review; if (invite) return DoneSlot.invite; if (!check.eligible) return DoneSlot.none; return DoneSlot.interstitial; 

The ad is last. And it is only loaded once it has won the slot, so a user who is about to be asked for a review never pays for an ad download in the background.

What I would tell past me

  • Write the range when you add the key, not after the first incident.
  • Fall back to the shipped default, never to zero.
  • Keep "how often" floors in code, and let the console only make things gentler.
  • Test the parser with junk: empty string, negative, float, "abc", huge number. Those tests take five minutes and they are the only review a console change ever gets.

The app is Duplicate Photos Remover on Google Play. Everything runs on the phone and deletes only go to the system Trash. If you want to see these rules from the user side: https://www.pipex.pro/get/photo-cleaner?utm_source=devto&utm_campaign=pc117&utm_content=rc_clamps

A question for anyone shipping Remote Config: do you clamp on the client like this, or do you validate on the server side before a change can be published? I'd like to know what has bitten you.

原文:https://www.a8q9.cn/en/article/remote-config-without-foot-guns-clamping-every-knob-in-a-flutter-app-0171a8.html

相关文章