loreq

Last updated:

0 purchases

loreq Image
loreq Images
Add to Cart

Description:

loreq

Future based Http request package for the Dart and browser. It is inspired by axios package in Node.js



Install #
# recommened install
dart pub add loreq
copied to clipboard
Usage #
Import loreq in your file.
import 'package:loreq/loreq.dart';

void main() {
loreq('https://example.com/cars?brand=bmw')
.then((data) => print(data));
}
copied to clipboard
And now we got the bmw brands from /cars on our server and printed them on the terminal. How simple isn't it?
import 'package:loreq/loreq.dart';

void main() {
loreq('https://example.com/cars', params: {'brand': 'bmw'})
.then((data) => print(data));
}
copied to clipboard
Post, Put and Delete requests #
When using the loreq function, the get request is automatically thrown if no method is specified. However, you can use other request types if you want.
Post
import 'package:loreq/loreq.dart';

void main() {
loreq('https://example.com/users/new', params: {'id': 12345, 'username': 'JohnDoe'} method: 'POST')
.then((data) => print(data));
}
copied to clipboard
Put
import 'package:loreq/loreq.dart';

void main() {
loreq('https://example.com/password/change', params: {'id': 12345, 'newPassword': 'himom'} method: 'PUT')
.then((data) => print(data));
}
copied to clipboard
Delete
import 'package:loreq/loreq.dart';

void main() {
loreq('https://example.com/users/delete', params: {'id': 12345, 'password': 'himom'} method: 'DELETE')
.then((data) => print(data));
}
copied to clipboard
Browser #
If you want to use Loreq in normal Javascript, you don't need to convert your Dart code to Javascript code. Loreq already comes with a file named browser.js.
// ES6+
import loreq from 'packages/loreq/web/browser.mjs'
// CommonJS (for Node.js)
const loreq = require('packages/loreq/web/node.js')

loreq('https://example.com/packages?id=loreq')
.then(data => console.log(data))
copied to clipboard
You can also use post, put and delete requests as you would in Dart.
// ES6+
import loreq from 'packages/loreq/web/browser.mjs'
// CommonJS (for Node.js)
const loreq = require('packages/loreq/web/node.js')

loreq('https://example.com/packages/new', {
params: { name: 'loreq', creatorToken: /* ... */},
method: 'POST'
}).then(data => console.log(data))
copied to clipboard
Errors #
You can access data['error'] to see if the response after a request returns an error.
import 'package:loreq/loreq.dart';

void main() {
loreq('https://example.com/packages/aPackageForFake') // An incorrect request
.then((data) {
if (data['error']) {
print('Something went wrong')
} else {
// ...
}
})
}
copied to clipboard
Example #
See example for see usage example
Changelog #
See changelog for see changelog

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.