rest_api_event

Creator: coderz1093

Last updated:

0 purchases

rest_api_event Image
rest_api_event Images
Add to Cart

Description:

rest api event

Solution for calling APIs #

Get started #

Specify the path to your service:

Provider.url = "https://localhost:8080/rest/";
copied to clipboard
ApiEvent #
Used to get a response from a service call and control server events.

Create a model for data with a static method that parses the response body into the data you want (this parser will be called in another Isolate):
static T parser(String body)

class Todo {
String title;

Todo.fromJson(Map<String, dynamic> json) {
title = json["title"];
}

static List<Todo> parser(String body) {
List json = convert.json.decode(body);
List<Todo> todos = [];
for (Map object in json) todos.add(Todo.fromJson(object));
return todos;
}
}
copied to clipboard

Create an ApiEvent for the desired service:

ApiEvent<List<Todo>> todos = ApiEvent(
service: "todos",
httpMethod: HttpMethod.GET,
parser: Todo.getParser,
/// (optional)
/// saveAuthToken: if request is successful -> save the authorization token from the cookie,
/// auth: use saved authorization token in the header
);
copied to clipboard

Just run event:

todos.run({params, body}); /// (optional) params, body
copied to clipboard
Event view #

Event UI View:

EventBuilder(
event: todos,
completed: (data) => ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) => Text(data[index].title),
),
/// (optional)
/// initial: widget at initialization, if not specified, then loading
/// loading: widget on call
/// error: function with an error message that returns widget
),
copied to clipboard

Full example of a widget to get a todo list from https://localhost:8080/rest/todos service:

class _AwaitWidgetState extends State<AwaitWidget> {
final ApiEvent<List<Todo>> todos = ApiEvent(
service: "todos", httpMethod: HttpMethod.GET, parser: Todo.getParser);

@override
void initState() {
todos.run();
super.initState();
}

@override
Widget build(BuildContext context) {
return EventBuilder(
event: todos,
completed: (data) => Center(
child: ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) => Text(data[index].title))),
);
}
}
copied to clipboard
Event #
Used to control client events.

Definition of an event reacting to changes and its view:

Event<bool> event = Event<bool>();
copied to clipboard
EventBuilder(event: event, builder: (context, data) => Text(data.toString()))
copied to clipboard

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.

Related Products

More From This Creator