DEV Community

Cover image for Agri Ops: building a farm app that never asks for the internet
Stacklyn Labs
Stacklyn Labs

Posted on

Agri Ops: building a farm app that never asks for the internet

The release build of Agri Ops doesn't request the INTERNET permission.

That one line in the Android manifest is the clearest summary of the app we could give you. Agri Ops is our Flutter app for farmers and small agri businesses: fields and soil types, crops from planting to harvest, harvest yields, and what every bit of farm work cost. Farms are where mobile signal goes to die, so we built it for airplane mode first. There's no backend, no account and no API key. Fonts are bundled. Nothing is downloaded at runtime.

Offline-first is the easy part to say. The interesting problems were the ones that come with it, because with no server there is no second copy of anything. Here's what we did about three of them.

Problem: the yields don't add up

A farmer logs one harvest in Kg because that's what the scale says. Another in Tons from the buyer's ticket. A third in Bushels, because that's how that crop is traded locally. Then they look at the dashboard total.

Before version 1.1, adding those raw numbers was exactly the bug. Now every yield is converted to kilograms first, summed, and then shown in whichever unit the user picks for totals (Kg, Tons or Lbs).

// simplified
enum YieldUnit { kg, tons, lbs, bushels }

double toKg(double qty, YieldUnit unit) => switch (unit) {
      YieldUnit.kg => qty,
      YieldUnit.tons => qty * 1000,
      YieldUnit.lbs => qty * 0.45359237,
      YieldUnit.bushels => qty * 27.2, // approximate
    };

double totalKg(Iterable<YieldLog> logs) =>
    logs.fold(0, (sum, y) => sum + toKg(y.quantity, y.unit));
Enter fullscreen mode Exit fullscreen mode

The bushel line deserves its comment. A bushel is a volume, and what it weighs depends on the crop. The app uses an approximation of about 27.2 kg and says so, rather than pretending to a precision it doesn't have. If you adapt Agri Ops for a single crop, that's the first constant we'd suggest replacing.

Agri Ops dashboard with total fields, harvest yield, costs and active crops

Problem: statistics that drift from the data

The dashboard shows total yield, total cost and active crops. The tempting way to build that is a set of counters you bump on every save. Counters drift. Miss one code path and the dashboard is wrong until someone reinstalls.

We went the other way. There's one provider per data type, and a FarmStatsProvider that is rebuilt from crops, yields and activities whenever any of them changes, using Provider's ProxyProvider:

// simplified
ProxyProvider3<CropProvider, YieldProvider, ActivityProvider, FarmStats>(
  update: (_, crops, yields, activities, __) => FarmStats.from(
    crops: crops.items,
    yields: yields.items,
    activities: activities.items,
  ),
)
Enter fullscreen mode Exit fullscreen mode

All writes go through the providers and a single DatabaseService. That gives us one door to guard. If the database can't be opened, DatabaseService shows a recovery screen instead of crashing. And if a client later wants cloud sync, that service is where it would plug in. Sync isn't in the template; it would be custom work.

Crop status helps the numbers too. A crop can be marked Harvested or Failed, so the Active count stays honest, and logging a yield marks the crop Harvested on its own.

Agri Ops harvest yield log and farm activity cost tracking

Problem: one wrong tap, and there's no server to ask

On a cloud app, an accidental delete is a support ticket. Here it would just be gone. So we made destructive actions slow and reversible.

Deleting a field asks first, and it cascades: the field's crops, their yields and its activities all go with it. Undo puts every one of them back. The test suite covers that cascade-and-undo path specifically.

Backups export everything to a JSON file through the system share sheet, so Drive, Files or email. Restore is where we were most careful. The file is checked completely before anything is written, then the write is all-or-nothing, and a failed restore is rolled back.

// simplified
Future<void> restore(String json) async {
  final backup = Backup.parse(json); // throws on a damaged file,
                                     // another app's file or a newer version
  final before = await db.exportAll();
  try {
    await db.replaceAll(backup);
  } catch (_) {
    await db.replaceAll(before); // roll back
    rethrow;
  }
}
Enter fullscreen mode Exit fullscreen mode

Backups from version 1.0 still restore. Files from another app, a newer version, or a damaged file are rejected with a clear message.

Harvest reminders follow the same offline rules. They fire on the harvest day or 1, 3, 7 or 14 days before it (3 by default), at 9:00 in the device's time zone, for every crop still growing. They're rebuilt whenever crops change and after a reboot or app update. Notification permission is only asked for when the user turns reminders on.

What you won't find

Not included Why it matters
Charts Totals are cards on the dashboard, not graphs
Cloud sync, user accounts Data lives on one device; backups move it
Ads, in-app purchases Nothing to strip out, nothing to monetise yet
Web, Windows, macOS, Linux builds Android and iOS only

The stack: Flutter 3.47 or newer, Dart 3.9+, Provider, Hive CE with adapters already generated, iOS 15.0 and up, 35 unit and widget tests.

Try it

The source is $29.

For anyone who has built for farms or other no-signal places: what broke first when your app met real field conditions?

Top comments (1)

Collapse
 
__d03cc26214f3 profile image
איתמר וענונו •

"No INTERNET permission" as the one-line summary of the product is great. I run a construction business and job sites are the same story: half the time there's no signal exactly where you need to log something. The units bug is very familiar too - in construction it's m² vs m³ vs running metres, and the totals look fine until someone adds them. One question about the no-backend choice: with no second copy, how do you handle a farmer who drops the phone in a trough? Do you offer a manual export/backup file, or is losing the device an accepted risk for now?