json_data_loader 1.0.2
json_data_loader: ^1.0.2 copied to clipboard
A lightweight JSON loader for Flutter apps. Load JSON from assets or network and map it to your models.
json_data_loader #
A lightweight and flexible JSON loader for Flutter apps.
Load JSON from local assets or network APIs and map it directly to your Dart models.
Features #
- Load JSON from assets (local files) or network (HTTP URLs)
- Map JSON to any Dart model via
fromJsonfunction - Supports List and Map JSON structures
- No caching — always fetch fresh data
- Uses compute() to parse large JSON without blocking the UI
- Flutter compatible (uses
rootBundlefor assets) - Easy integration in any Flutter project
Installation #
Add the dependency in your pubspec.yaml:
dependencies:
json_data_loader: ^1.0.0
Then run:
flutter pub get
Quick Start / Usage #
1️⃣ Load a list of models from a local asset #
import 'package:json_data_loader/json_data_loader.dart';
import 'posts_model.dart';
final posts = await JsonDataLoader.loadList<PostsModel>(
source: JsonSource.asset('assets/posts.json'),
fromJson: (json) => PostsModel.fromJson(json),
);
2️⃣ Load a list of models from a network API #
final posts = await JsonDataLoader.loadList<PostsModel>(
source: JsonSource.network('https://jsonplaceholder.typicode.com/posts'),
fromJson: (json) => PostsModel.fromJson(json),
);
3️⃣ Load a single object from JSON #
final user = await JsonDataLoader.loadObject<UserModel>(
source: JsonSource.network('https://example.com/user/1'),
fromJson: (json) => UserModel.fromJson(json),
);
Example Flutter Widget #
class PostsPage extends StatelessWidget {
const PostsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Posts')),
body: FutureBuilder<List<PostsModel>>(
future: JsonDataLoader.loadList<PostsModel>(
source: JsonSource.network(
'https://jsonplaceholder.typicode.com/posts'),
fromJson: (json) => PostsModel.fromJson(json),
),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(child: Text('No data'));
}
final posts = snapshot.data!;
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, i) {
final post = posts[i];
return ListTile(
title: Text(post.title),
subtitle: Text(post.body),
);
},
);
},
),
);
}
}
Model Example #
class PostsModel {
final int userId;
final int id;
final String title;
final String body;
PostsModel({
required this.userId,
required this.id,
required this.title,
required this.body,
});
factory PostsModel.fromJson(Map<String, dynamic> json) => PostsModel(
userId: json["userId"],
id: json["id"],
title: json["title"],
body: json["body"],
);
}
Example Folder #
You can check a full working example in the example/ folder of this package.
It demonstrates how to load JSON from both assets and network.
Notes #
- Ensure your JSON structure matches the model you are mapping.
- Large JSON files are parsed using
compute()to avoid UI blocking. - No caching: every call fetches fresh data from the source.
- Works with both
List<dynamic>andMap<String, dynamic>JSON structures.
Contributing #
Contributions are welcome!
Fork the repository and submit pull requests for improvements or bug fixes.
License #
This project is licensed under the MIT License.