nui_web 1.0.4 copy "nui_web: ^1.0.4" to clipboard
nui_web: ^1.0.4 copied to clipboard

A new Flutter package for web/API calling


id: flutter-sdk-nuiweb title: NUIWeb Overview sidebar_label: Overview #

NUIWeb is part of the Flutter SDK that allows you to integrate HTTP calls into the app in a simpler way:

  • Generalized Base URL - Specify the base URL during the builder method for the app and start calling the HTTP methods by only specifying the path

  • Generalized Headers - Specify the common headers and they be auto-injected into all the HTTP calls made throughout the app.

  • Customizable Error Message - All HTTP errors are caught automatically and a standard response with the error code and message are returned. Customize the error messages for different HTTP error codes.

  • Pre/Post HTTP Handling - Add additional processing to the response before and after the HTTP callback is made.

Builder Method for NUIWeb #

Method Remark
module() Specify the module of this NUIWeb instance.
baseURL() Specify the base URL of this instance.
headers() Specify the common headers for this instance.
statusMessages() Customize the messages for different HTTP status code.
preHandler() Add additional pre-processing to the response.
postHandler() Add additional post-processing to the response.
build() Build the NUIWeb instance with the specified module.

Initialize NUIWeb with NUIWebBuilder. #

Create a NUIWebBuilder instance to build NUIWeb instance.

var web = NUIWebBuilder();

Specifying the module for NUIWeb. #

On the builder method, users can specify the module of the NUIWeb instance to have multiple NUIWeb instance for the app.

builder.module("hintv2");

Specifying the base URL for NUIWeb. #

On the builder method, users can specify the base URL of the NUIWeb instance so that this url is used throughout all the HTTP calls for this NUIWeb instance.

builder.module("https://uathintv2.honda.net.my/hint-api/");

Specifying the common headers for NUIWeb. #

On the builder method, users can specify the common headers so that these headers are automatically added to all the HTTP calls for this NUIWeb instance.

builder.headers(() => {
      "authorization" : "Bearer aasdjfhajsdfkjhkahs",
    });
  • Parameters:
    Name Type Nullable Remark
    headers HttpHeaderSetter N A callback to obtain the headers in Map<String, String> format

Specifying customized HTTP code messages. #

On the builder method, users can specify custom messages for different HTTP codes to all the HTTP calls for this NUIWeb instance.

builder.statusMessages({
    401: "Oops, you are unauthorized to access this resource",
    404: "Oops, the server is temporarily down for maintenance"
});

Adding the Pre/Post-Response Handler #

Users can add a pre-response or post-response handler to handle additional tasks before returning the response in the callback.

builder.preHandler((response) { 
  //Todo what you want here
});
builder.postHandler((response) { 
  //Todo what you want here
}, delay: Duration(milliseconds: 500));
  • Parameters:
    Name Type Nullable Remark
    handler HttpAdditionalHandler N A callback to handle the response
    delay Duration Y The duration of delay for post handler

Build the NUIWeb instance #

Once all the customizations for this NUIWeb is done, building the instance with NUIWebBuilder will return the instance of the NUIWeb.

final web = builder.build();

A Complete Example for Building A NUIWeb Instance #

final web = NUIWebBuilder()
        .module("app")
        .headers(() => {
          "authorization": "Bearer token here",
          "x-tenant-id": "My Tenant Id"
        })
        .baseURL("http://143.192.96.7:8080")
        .statusMessages({
          404 : "Oops, this page you're looking for is not found",
          500: "Oops, seems like something went wrong"
        })
        .build();

Available Methods for NUIWeb #

Method Remark
get() Get the instance of the built NUIWeb instance
doGET() Call a GET method to the path specified
doPOST() Call a POST method to the path specified
doPATCH() Call a PATCH method to the path specified
doPUT() Call a PUT method to the path specified
doDELETE() Call a DELETE method to the path specified

Getting the NUIWeb Instance #

Users get the built NUIWeb instance for the targetted module.

final web = NUIWeb.get(module: "hintv2")

Calling a GET method #

  • Parameters:
    Name Type Nullable Remark
    url String N The targetted url (This is appended to the base URL if specified)
    headers Map<String, String> Y Specific headers for this HTTP request, common headers are included
    pathVariable String Y Path variable if any
    params Map<String, String> Y Request parameters if any
    entity NUIEntMapper Y Then entity object to decode the response body

Example of a GET method #

final getResult = NUIWeb.get(module: "app").doGET(url: "cases", params: {
      "month": 2.toString(),
      "year": 2020.toString()
    });

Calling a POST method #

  • Parameters:
    Name Type Nullable Remark
    url String N The targetted url (This is appended to the base URL if specified)
    headers Map<String, String> Y Specific headers for this HTTP request, common headers are included
    pathVariable String Y Path variable if any
    body Map<String, dynamic> Y Request body in map if any
    bodyEnt extends NUIEnt Y Request body in object format if any
    entity NUIEntMapper Y Then entity object to decode the response body
    encoding Encoding Y Encoding type for the request

Example of a POST method #

final postResult = NUIWeb.get(module: "app").doPOST(url: "cases", pathVariable: "1004", body: {
      "month": 2.toString(),
      "year": 2020.toString()
    });

Calling a PUT method #

  • Parameters:
    Name Type Nullable Remark
    url String N The targetted url (This is appended to the base URL if specified)
    headers Map<String, String> Y Specific headers for this HTTP request, common headers are included
    pathVariable String Y Path variable if any
    body Map<String, dynamic> Y Request body in map if any
    bodyEnt extends NUIEnt Y Request body in object format if any
    entity NUIEntMapper Y Then entity object to decode the response body
    encoding Encoding Y Encoding type for the request

Example of a PUT method #

final putResult = NUIWeb.get(module: "app").doPUT(url: "cases", body: {
      "month": 2.toString(),
      "year": 2020.toString()
    });

Calling a PATCH method #

  • Parameters:
    Name Type Nullable Remark
    url String N The targetted url (This is appended to the base URL if specified)
    headers Map<String, String> Y Specific headers for this HTTP request, common headers are included
    pathVariable String Y Path variable if any
    body Map<String, dynamic> Y Request body in map if any
    bodyEnt extends NUIEnt Y Request body in object format if any
    entity NUIEntMapper Y Then entity object to decode the response body
    encoding Encoding Y Encoding type for the request

Example of a PATCH method #

final patchResult = NUIWeb.get(module: "app").doPATCH(url: "cases", pathVariable: "1005", body: {
      "month": 2.toString(),
      "year": 2020.toString()
    });

Calling a DELETE method #

  • Parameters:
    Name Type Nullable Remark
    url String N The targetted url (This is appended to the base URL if specified)
    headers Map<String, String> Y Specific headers for this HTTP request, common headers are included
    pathVariable String Y Path variable if any
    entity NUIEntMapper Y Then entity object to decode the response body
    encoding Encoding Y Encoding type for the request

Example of a DELETE method #

final deleteResult = NUIWeb.get(module: "app").doDELETE(url: "cases", pathVariable: "1002");
1
likes
70
pub points
68%
popularity

Publisher

unverified uploader

A new Flutter package for web/API calling

Homepage

Documentation

API reference

License

unknown (LICENSE)

Dependencies

connectivity_plus, flutter, http, nui_core

More

Packages that depend on nui_web