Site icon Flutter Packages | Pub dev Packages – Flutter Mobile App World

Network request result

session

Network request result

json to dart https://javiercbk.github.io/json_to_dart/

Getting Started

Add dependency

dependencies:
  session: ^1.0.3+2  #latest version

Example

import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:session/session.dart';

export 'package:session/session.dart' show Result;

Config configAPI(String baseURL) {
  return Config(
    baseUrl: baseURL ?? "https://www.httpbin.org/",
//    proxy: 'PROXY localhost:8888',
    connectTimeout: 10,
    receiveTimeout: 10,
  );
}

SessionInterceptorSendHandler _onRequest = (options) async {
  var headers = {
    'os': Platform.isIOS ? 'ios' : 'android',
  };
  options.headers.addAll(headers);
  // if (UserStore.store.getState().isLogin) {
  //   options.headers['id'] = UserStore.store.getState().user.userId;
  // }
  // options.contentType = Headers.formUrlEncodedContentType;
  // options.responseType = ResponseType.plain;
  return options;
};

/// Response result interception processing
Result _onValidResult(Result result, bool validResult, BuildContext context) {
  // 拦截处理一些错误
  if (validResult) {
    switch (result.code) {
      case "${-3}":
        // do something...
        break;
      case "${-2}":
        // do something...
        break;
    }
  }
  return result;
}

/// 
/// Send the request and parse the entity type corresponding to the result returned by the remote server 
/// 
/// baseUrl: host address 
/// path: request path 
/// queryParameters: URL carries request parameters 
/// connectTimeout: Timeout 
// validResult: Whether to verify the return result 
/// context: context 
///
Future<Result> getAPI(
    {String baseUrl,
    String path = '',
    Map<String, dynamic> queryParameters,
    int connectTimeout,
    bool validResult = true,
    BuildContext context}) async {
  return requestAPI(
      baseUrl : baseUrl,
      path: path,
      queryParameters: queryParameters,
      options: Options(method: 'get'),
      connectTimeout: connectTimeout,
      validResult : validResult,
      context: context);
}

/// 
/// Send a request and parse the entity type corresponding to the result returned by the remote server 
/// 
/// baseUrl: host address 
/// path: request path 
/// data: request parameters 
// connectTimeout: timeout 
/// validResult: whether to verify the return result 
/// context: context 
///
Future<Result> postAPI(
    { String baseUrl,
     String path =  '' ,
    data,
    int connectTimeout,
    bool validResult = true,
    BuildContext context}) async {
  return requestAPI(
      baseUrl : baseUrl,
      path: path,
      date : date,
      options: Options(method: 'post'),
      connectTimeout: connectTimeout,
      validResult : validResult,
      context: context);
}

/// 
/// Send the request and parse the entity type corresponding to the result returned by the remote server 
/// 
/// baseUrl: host address 
/// path: request path 
/// data: request parameters 
/// validResult: whether to verify Return result 
/// context: context 
///
Future<Result> requestAPI(
    { String baseUrl,
     String path =  '' ,
    data,
    Map<String, dynamic> queryParameters,
    Options options,
    int connectTimeout,
    bool validResult = true,
    BuildContext context}) async {
  Session session = Session(
    config :  configAPI (baseUrl),
    onRequest: _onRequest,
  );
  Result result = await session.request(
    path,
    date : date,
    queryParameters: queryParameters,
    options: options,
    connectTimeout: connectTimeout,
  );
  return _onValidResult(result, validResult, context);
}

void example() async {
  getAPI(path: "ip").then((result) {
    print("======");
    print(result.code);
    print(result.message);
    print(result.body);
    print("======");
  });
}

Contribute on GitHub

https://github.com/OctMon/session

Exit mobile version