Last updated:
0 purchases
simple fetch
simple_fetch #
A wrapper around Dio. This make simple and reduce the complexities
of working with models and http requests in Dart/Flutter.
Get started #
Install #
Add the simple_fetch package to your
pubspec dependencies.
import 'package:simple_fetch/simple_fetch.dart';
final simpleFetch = SimpleFetch();
String apiUrl = 'https://jsonplaceholder.typicode.com/todos/';
copied to clipboard
Very simple use #
testGet()async{
List<Todo?> allProductsData = await simpleFetch.getList<Todo>(
url: apiUrl,
mapper: (json) => Todo?.fromJson(json),
);
}
copied to clipboard
Examples #
Performing a GET request :
/// Fetch an object and make it a model type
void getOneDataItemFromEndpoint() async {
SimpleFetch simpleFetch = SimpleFetch();
String apiUrl = 'https://jsonplaceholder.typicode.com/todos/1';
try {
Todo? productsData = await simpleFetch.get<Todo>(
url: apiUrl,
mapper: (json) => Todo?.fromJson(json),
// transformer: (transform) => transform['data'],
);
print(productsData?.toJson());
} on SimpleError catch (exception) {
print(exception.message);
} catch (e) {}
}
/// Fetch a List of object and make it a model type
void getDataFromEndpoint() async {
SimpleFetch simpleFetch = SimpleFetch();
String apiUrl = 'https://jsonplaceholder.typicode.com/todos/';
try {
List<Todo?> allProductsData = await simpleFetch.getList<Todo>(
url: apiUrl,
mapper: (json) => Todo?.fromJson(json),
);
print(allProductsData.map((e) => e?.toJson()).toList());
} on SimpleError catch (exception) {
print(exception.message);
} catch (e) {}
}
copied to clipboard
Performing a POST request:
/// If the response of your post request is a single item
void postSingleResponse() async {
SimpleFetch simpleFetch = SimpleFetch();
String apiUrl = 'https://jsonplaceholder.typicode.com/todos/1';
try {
Todo? productsData = await simpleFetch.post<Todo>(
url: apiUrl,
data: {'id': 1, 'name': 'hello'},
mapper: (json) => Todo?.fromJson(json),
// transformer: (transform) => transform['data'],
);
print(productsData?.toJson());
} on SimpleError catch (exception) {
print(exception.message);
} catch (e) {}
}
// If the response of your post request is a list of item
void postMultipleResponse() async {
SimpleFetch simpleFetch = SimpleFetch();
String apiUrl = 'https://jsonplaceholder.typicode.com/todos/';
try {
List<Todo?> allProductsData = await simpleFetch.postList<Todo>(
url: apiUrl,
data: {'id': 2, 'name': 'Hiiii'},
mapper: (json) => Todo?.fromJson(json),
);
print(allProductsData.map((e) => e?.toJson()).toList());
} on SimpleError catch (exception) {
print(exception.message);
} catch (e) {}
}
copied to clipboard
Performing multiple concurrent requests:
response = await Future.wait([request1, request2]);
copied to clipboard
Transformers #
By default the packages is designed for you to simply retrieve data that is a list or json
Sometimes you may want to remove,add or filter the data that is received before making it an object
The transformer parameter helps you achieve this
{
"data":[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 2,
"id": 2,
"title": "User ovier vac",
"completed": true
},
]
}
copied to clipboard
In this case you may want to return an list so you can do
try {
List<Todo?> allProductsData = await simpleFetch.getList<Todo>(
url: apiUrl,
mapper: (json) => Todo?.fromJson(json),
transformer: (transform) => transform['data'],
);
print(allProductsData.map((e) => e?.toJson()).toList());
} on SimpleError catch (exception) {
print(exception.message);
} catch (e) {}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.