request_api_helper 3.0.1+6 copy "request_api_helper: ^3.0.1+6" to clipboard
request_api_helper: ^3.0.1+6 copied to clipboard

outdated

integrating API will be faster and doesn't take long, just configure the initial settings and you can use this helper freely.

Request Api Helper #

one code, one style for all restapi;

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  request_api_helper:

Usage #

import 'package:request_api_helper/request.dart' as req;

New Feature #

onUploadProgress :

import 'package:request_api_helper/request.dart' as req;
import 'package:request_api_helper/request_api_helper.dart' show RESTAPI; // use show [class] for selection 

upload() async {
  await req.send(
    name: 'upload',
    type: RESTAPI.POST,
    data: RequestData(
      body: {},
      file: RequestFileData(
        filePath: [test.path], // list of path
        fileRequestName: ['test'], // list of name request 
      ),
    ),
    changeConfig: RequestApiHelperConfigData(
      onSuccess: (response) async {
        // important
        await Session.save('token', 'Bearer ' + response['token']);
      },
    ),
    onUploadProgress: (sended, total) {
      // always update in every upload size
      print(sended);
      print(total);
      setState((){});
    }
  );
}

1 . Setting Config #

use static Env in main like this (Release):

import 'package:request_api_helper/model/config_model.dart';

void main(
  RequestApiHelperConfig.save(
    RequestApiHelperConfigData(
      url: 'https://official-joke-api.appspot.com/api/',
      noapiurl: 'https://official-joke-api.appspot.com/',
      logResponse: true,
      withLoading: Redirects(toogle: false),
      
    ),
  );
  runApp(MyApp());
}

2 . Make Login And Saving Token #

First you must make login request.

login() async {
======================= GET DATA FROM SERVER
  await req.send(
    name: 'login',
    type: RESTAPI.POST,
    data: RequestData(
      body: {
        'username' : username,
        'password' : password,
      }
    ),
    changeConfig: RequestApiHelperConfigData(
      onSuccess: (response) async {
        // important
        await Session.save('token', 'Bearer ' + response['token']);

        // auto save different type data
        await Session.save('user_name', response['user']['name']);
        await Session.save('user_id', response['user']['id']);
      },
    ),
  );
}

other request

login() async {
======================= GET DATA FROM SERVER
  await req.send(
    name: 'other/name',
    type: RESTAPI.POST,
    data: RequestData(),
    changeConfig: RequestApiHelperConfigData(
      onSuccess: (response) async {
        // other function after success 
    ),
  );
}

3 . What is Session? #

session is Shared preferences plugin, implement :

save to Shared Preferences

await Session.save('myname','MIAUW'); // String
await Session.save('myint',100); // int
await Session.save('mybool',false); // bool

load from Shared Preferences

String mystring = await Session.load('myname'); // String
int myint = await Session.load('myint'); // int
bool mybool = await Session.load('mybool'); // bool

6
likes
0
pub points
25%
popularity

Publisher

unverified uploader

integrating API will be faster and doesn't take long, just configure the initial settings and you can use this helper freely.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, fluttertoast, http, http_parser, shared_preferences

More

Packages that depend on request_api_helper