mockrec

Record API once, run your Flutter app without backend.
The ultimate Dio interceptor for offline development, testing, and flawless demos.

pub version Dart 3 Flutter License: MIT


๐Ÿ›‘ The Problem

Building Flutter apps connected to real APIs is often frustrating:

  • โŒ Backend is down or returning 500 errors.
  • โŒ API contracts change, breaking your UI mid-development.
  • โŒ Client demos fail because the network is slow or unavailable.
  • โŒ Automated tests are flaky and depend too much on external servers.

โœจ The Solution

mockrec completely eliminates these headaches with a single line of code.

It works like a magic tape recorder for your network requests:

  1. ๐Ÿ”ด Record (Normal Mode): Browse your app normally. mockrec silently saves every successful API response into memory.
  2. ๐Ÿ” Replay (Mock Mode): Switch modes, and your app instantly replays those exact responses locally. No internet connection required!

Work faster, stabilize your development environment, and deliver flawless offline demos without writing complex mock JSON files.


๐Ÿš€ Key Features

  • โšก Zero Configuration โ€” Just pass your Dio instance. No complex setup.
  • ๐Ÿ’พ Auto-Recording โ€” Automatically captures real responses, status codes, and headers.
  • โœˆ๏ธ Airplane Mode Ready โ€” Run your app completely offline using recorded data.
  • ๐Ÿงช Perfect for Testing โ€” Isolate your UI tests from the backend effortlessly.
  • ๐Ÿชถ Ultra Lightweight โ€” Pure Dart. Only depends on dio.

๐Ÿ“ฆ Getting Started

Installation

Add the package to your pubspec.yaml:

dependencies:
  mockrec: ^0.0.2
  dio: ^5.4.0
flutter pub get

โšก Quick Start

import 'package:dio/dio.dart';
import 'package:mockrec/mockrec.dart';

void main() async {
  final dio = Dio();

  // 1. Attach the interceptor
  Mockrec.enable(dio);

  // 2. Fetch data normally (it silently records the response!)
  await dio.get("https://jsonplaceholder.typicode.com/users/1");

  // 3. Enable Mock Mode
  Mockrec.setMockMode(true);

  // 4. Fetch again โ€” Instant response, zero network calls!
  final response = await dio.get("https://jsonplaceholder.typicode.com/users/1");
  print(response.data);
}

๐Ÿ› ๏ธ API Reference

Mockrec.enable()

Parameter Type Default Description
dio Dio โ€” The Dio instance to attach the recording interceptor to

Attaches the recording interceptor to your Dio instance. This must be called before any API requests are made.

Mockrec.setMockMode()

Parameter Type Default Description
value bool โ€” true to enable mock replay, false to enable live recording

Toggles the core engine. When set to true, requests will never hit the network. If no mock data exists for an endpoint, a clear DioException is thrown to let you know.

Mockrec.isMockMode

Property Type Description
isMockMode bool Returns true if mock mode is currently active

Mockrec.clear()

Instantly clears all recorded mock data from memory. Extremely useful for resetting states between test runs or when switching user accounts.


๐Ÿ—๏ธ Architecture

lib/
 โ”œโ”€โ”€ mockrec.dart        โ† Barrel export
 โ””โ”€โ”€ src/
      โ”œโ”€โ”€ mock_interceptor.dart โ† Dio interceptor engine
      โ”œโ”€โ”€ mockrec_impl.dart โ† Public static API
      โ”œโ”€โ”€ mock_storage.dart     โ† In-memory storage & state
      โ””โ”€โ”€ utils.dart            โ† Fast Cache Key generator

๐Ÿ“‹ Requirements

Requirement Version
Dart SDK >=3.0.0 <4.0.0
Flutter >=3.10.0
Null safety โœ…
Dependencies dio: ^5.4.0

๐Ÿ’– Support

If this package saves you time and frustration, consider supporting the development. Your support helps keep this package maintained and up-to-date!

Support on SociaBuzz

Every contribution is greatly appreciated! ๐Ÿ™


๐Ÿ“„ License

MIT โ€” see LICENSE for details.

mockrec

Libraries

mockrec
A lightweight Dio interceptor that records real API responses and replays them without hitting the backend.