simple_rest 0.4.1
simple_rest: ^0.4.1 copied to clipboard
Get your REST API server up and running in no time with our user-friendly quickstart package, designed for easy initialization in just a few simple steps.
simple_rest #
simple_rest is a Dart package that provides a simple way to create RESTful APIs in Dart. #
Installation #
To install simple_rest, add the following dependency to your pubspec.yaml file:
dependencies:
simple_rest: ^0.4.1
Or just type on console:
dart pub add simple_rest
Then run dart pub get on the command line.
Use #
import 'package:simple_rest/simple_rest.dart';
Estructure (Opcional) #
Initially, the example suggests a folder structure to follow some best practices when building the system, but you can structure it however you prefer.
model
crud
repository
service
controller
main.dart
How to create Endpoints:
/// Call the methods we need and use them in our controller functions.
/// Declare controller using [RestController] + path
/// USE:
/// GetMapping
/// PostMapping
/// DeleteMapping
/// PatchMapping
/// PutMapping
/// Request its obligatory parameter for
@RestController('/school')
class SchoolController {
Request request;
SchoolController(this.request);
@GetMapping(path: '/id/<number>')
Map<String, dynamic> printSchoolIdWithNumber(@PathVariable('number') dynamic number) {
var token = request.headers['Authorization'];
return {'name': 'Name School', 'number': number, 'token':token};
}
@GetMapping(path: '/')
Map<String, dynamic> printSchoolIdWithNumber5() {
var token = request.headers['Authorization'];
return {'name': 'Name School', 'token':token};
}
@GetMapping(path: '/apellido/<ape>')
Map<String, dynamic> printSchoolIdWithName2(@PathVariable('ape') dynamic name) {
return {'address': 'J 23 Remuerandia', 'apellido': name};
}
@GetMapping(path: '/name/<name>')
Map<String, dynamic> printSchoolIdWithName(@PathVariable('name') dynamic name) {
return {'address': 'J 23 Remuerandia', 'school': name};
}
@PostMapping(path: '/')
List<Map<String, dynamic>> saveSchool(@BodyAttribute('body') dynamic bodyData) {
Map<String, dynamic> moreInfo = {
"school": "san jose"
};
var dataSchool = jsonDecode(bodyData );
return [
moreInfo,
dataSchool
];
}
@DeleteMapping(path: '/id/<id>')
Map<String, dynamic> deleteSchool(@PathVariable('id') String id){
List<String> dates = ['jhon','juan','pedro'];
dates.remove(id);
return {
"names": dates
};
}
@PatchMapping(path: '/')
Map<String, dynamic> updateSchool(@BodyAttribute('body') dynamic body){
var dataSchool = jsonDecode(body );
return dataSchool;
}
@PutMapping(path: '/')
Map<String, dynamic> updateSchoolComplete(@BodyAttribute('body') dynamic body){
var dataSchool = jsonDecode(body );
return dataSchool;
}
}
For initialize our server as follows and Just register the controllers on controllerList.
void main() {
Router app = Router();
SServer server = SServer();
server.start(
port: 8080,
isRouterControlActive: false,
app: app,
controllerList: [
SchoolController
],
);
}
This is how our server will be initialized and in my case, we can use our server as localhost on port 8080 with the endpoint user/all, which would look like this."https://localhost:8080/user/al"

using

Additional Notes. #
This mini library was created with only one purpose in mind - to be able to quickly and easily initialize your backend. That's why I made sure to use as few libraries as possible. This makes it lightweight and powerful, which improves the performance of our API. I tried to make the library easy to implement.
As a developer, you will likely find redundancy in some parts of the code, so I would appreciate it if you inform me in the most polite way possible. Remember, we can grow together 😘.