fe_network

A new Flutter package.

Getting Started

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Usage

FeNetworkManager在初始化时,是没有设置baseURL的。

FeNetworkManager
默认连接超时时间设置为30秒;
默认响应超时时间为120秒;
默认contentType为:Headers.jsonContentType;
默认responseType:ResponseType.json;

如果你的需求就是只有一个baseURL,则:

import 'package:fe_network/fe_network.dart';

class Url {
  static const baseUrl = 'https://xxx.com';
  static const listPath1 = '/app/v1/src/list1';
  static const listPath2 = '/app/v1/src/list2';
}

final _network = FeNetworkManager.shareInstance(baseUrl: Url.baseUrl);

class Api {
  Future<Map<String, dynamic>> requestListData1() async {
    final result = await _network.request(FeNetworkMethod.get, Url.listPath1);
    return result;
  }
  
  Future<Map<String, dynamic>> requestListData2() async {
    final result = await _network.request(FeNetworkMethod.get, Url.listPath2);
    return result;
  }
}

如果你不需要初始化时使用baseURL,自己匹配不同的baseURL,则:

import 'package:fe_network/fe_network.dart';

class ApiUrl {
  static const baseUrl1 = 'https://xxx.com';
  static const baseUrl2 = 'https://xx.xxx.com';
  static const listPath1 = '/app/v1/src/list1';
  static const listPath2 = '/app/v1/src/list2';
}

final _network = FeNetworkManager();

class Api {
  String _resultUrl(String path, {String baseUrl}) {
    return (baseUrl == null ? '' : baseUrl) + path;
  }
  
  String _baseUrl1(String path) {
    return _resultUrl(path, baseUrl: ApiUrl.baseUrl1);
  }
  
  String _baseUrl2(String path) {
    return _resultUrl(path, baseUrl: ApiUrl.baseUrl2);
  }
  
  Future<Map<String, dynamic>> requestListData1() async {
    final result = await _network.request(FeNetworkMethod.get, _baseUrl1(ApiUrl.listPath1));
    return result;
  }

  Future<Map<String, dynamic>> requestListData2() async {
    final result = await _network.request(FeNetworkMethod.get, _baseUrl2(ApiUrl.listPath2));
    return result;
  }
}

Libraries

fe_network
fe_network_entity
fe_network_error
fe_network_manager