jsoncare_flutter 🛡️

A production-ready, crash-proof JSON parsing helper and model generator for Flutter/Dart. Stop runtime crashes caused by unexpected API type changes!

🌟 Why JsonCare?

Mobile apps often crash when an API sends a String where an int was expected, or null instead of a List. JsonCare prevents these crashes by providing a tolerant parsing layer that safely converts types or provides fallsback defaults.


🚀 Features

  • Safe Parsing Engine: Never see type 'String' is not a subtype of type 'int' again.
  • Model Generator: Build null-safe, intolerant-to-crash model classes from raw JSON.
  • CLI Support: Generate models instantly from your terminal.
  • Nested Detection: Automatically handles deep JSON structures.
  • Null Safety: All generated fields are nullable and safely mapped.

🛠 Installation

Add to your pubspec.yaml:

dependencies:
  jsoncare_flutter: ^1.0.0

📖 Safe Parsing Engine (JsonCare)

Use the JsonCare utility inside your manual fromJson methods or anywhere you parse dynamic data.

import 'package:jsoncare_flutter/json_care.dart';

// Even if the API is inconsistent:
final dynamic data = {
  "id": "123",          // String instead of int
  "price": "19.99",     // String instead of double
  "is_active": 1,       // int instead of bool
  "tags": null,         // null instead of list
};

int id = JsonCare.intVal(data['id']);              // 123 (Auto-parsed)
double price = JsonCare.doubleVal(data['price']);  // 19.99 (Auto-parsed)
bool active = JsonCare.boolVal(data['is_active']); // true (1 -> true)
DateTime date = JsonCare.dateVal(data['date']);    // DateTime (Converted to Local)
List<String> tags = JsonCare.list(data['tags'], (i) => JsonCare.string(i)); // [] (Safe fallback)

🏗 Model Generator

Generate beautiful model classes in seconds.

Command Structure:

dart run jsoncare_flutter generate --name <RootClassName> --code '<RawJSON>'

Example:

dart run jsoncare_flutter generate \
  --name UserProfile \
  --code '{"id": 1, "name": "John", "address": {"city": "Dhaka"}}'

How to input JSON correctly:

  • Single Quotes: Wrap your JSON string in single quotes '{"key": "value"}'.
  • Escaping: If your JSON contains single quotes, use double quotes for the shell wrap and escape the interior double quotes, or better yet, use a file (see below).
  • Pro Tip (MacOS/Linux): If you have a file data.json, you can pass its content:
    dart run jsoncare_flutter generate --name Profile --code "$(cat data.json)"
    

2. Output Structure

The generator follows the Folder-per-Model rule:

  • input: --name UserProfile
  • output: lib/models/user_profile/
    • user_profile.dart
    • address.dart
    • models.dart (Barrel file)

🧪 Example Project

Check the Example Project for a full demonstration of safe parsing and programmatic generation.

📝 License

MIT License. Designed with ❤️ for the Flutter community.

🧑‍💻 Developer / ডেভেলপার

Md. Rahul Reza GitHub | LinkedIn | Website