RxNet class
author: ZhengZaiHong email: 1096877329@qq.com RxNet Plus - Flutter 网络请求库 一个全平台兼容(Android / iOS / Windows / Linux / macOS / Web / HarmonyOS)的 网络请求框架,支持缓存策略、多环境切换、并发请求等特性。
快速开始
// 1. 初始化(使用 RxNetConfig)
await RxNet.init(config:RxNetConfig(
baseUrl: "https://api.example.com",
cacheMode: CacheMode.CACHE_EMPTY_OR_EXPIRED_THEN_REQUEST,
interceptors: [RxNetLogAdapterInterceptor()],
));
// RESTful 请求 / RESTful request
await RxNet.get()
.setPath("/api/users/{id}/posts")
.setPathParam("id", "123")
.setQueryParam("page", 1)
.request();
// POST JSON 数据 / POST JSON data
await RxNet.post()
.setPath("/api/user")
.setBodyParams({"name": "John", "age": 25})
.asJson()
.request();
// 文件上传 / File upload
await RxNet.post()
.setPath("/api/upload")
.setBodyParam("file", multipartFile)
.asFormData()
.request();
初始化方式
// 方式1:直接构造 RxNetConfig
await RxNet.init(config:RxNetConfig(
baseUrl: "https://api.example.com",
cacheMode: CacheMode.FIRST_USE_CACHE_THEN_REQUEST,
cacheMaxSize: 500,
cacheEvictionPolicy: CacheEvictionPolicy.lru,
interceptors: [RxNetLogAdapterInterceptor()],
));
// 方式2:Builder 模式
await RxNet.init(config:RxNetConfig.builder()
.baseUrl("https://api.example.com")
.adapter(DioAdapter())
.cacheMode(CacheMode.CACHE_EMPTY_OR_EXPIRED_THEN_REQUEST)
.addInterceptor(RxNetLogAdapterInterceptor())
.cacheInvalidationTime(60 * 1000)
.cacheMaxSize(200)
.cacheEvictionPolicy(CacheEvictionPolicy.lfu)
.build());
示例: async-await 方式
回调方式
多环境切换
await RxNet.init(config:RxNetConfig(
baseUrl: "https://api.example.com",
baseUrlEnv: {
'dev': 'https://dev.api.example.com',
'test': 'https://test.api.example.com',
'release': 'https://api.example.com',
},
));
RxNet.setDefaultEnv('test');
并发请求
final results = await RxNet.zipRequest([
ZipRequest<UserInfo>(request: () async => getUser(), tag: 'user'),
ZipRequest<UserSettings>(request: () async => getSettings(), tag: 'settings'),
]);
Properties
- adapter → NetworkAdapter?
-
no setter
- baseUrl → String
-
no setter
- cacheManager ↔ RxNetCache
-
缓存管理器,提供缓存的读写、清理等高级操作
getter/setter pair
- debugManager ↔ DebugManager
-
latefinal
- hashCode → int
-
The hash code for this object.
no setterinherited
- logManager ↔ LogManager
-
latefinal
-
logsNotifier
→ ValueNotifier<
List< String> > -
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
deleteRequest<
T> () → BuildRequest< T> -
getAdapter(
) → NetworkAdapter? -
getBaseCacheMode(
) → CacheMode? -
getCacheInvalidationTime(
) → int -
getCheckNetWork(
) → CheckNetWork? -
getDatabase(
) → RxNetDataBase? -
getHeaders(
) → Map< String, dynamic> -
getIgnoreCacheKeys(
) → List< String> ? -
getRequest<
T> () → BuildRequest< T> -
headRequest<
T> () → BuildRequest< T> -
initNet(
{required RxNetConfig config}) → Future< void> -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
optionsRequest<
T> () → BuildRequest< T> -
patchRequest<
T> () → BuildRequest< T> -
postRequest<
T> () → BuildRequest< T> -
putRequest<
T> () → BuildRequest< T> -
setCollectLogs(
bool collect) → void -
setEnv(
String env) → void -
setHeaders(
Map< String, dynamic> header) → void -
toString(
) → String -
A string representation of this object.
inherited
-
zipRequestInstance(
List< ZipRequest> requests, {bool eagerError = true, CancelToken? cancelToken, Duration? timeout}) → Future<ZipResults> - 实例方法,用于执行具有多实例支持的并发请求。 Instance method for executing concurrent requests with multi-instance support.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
-
debugWindow
↔ ValueNotifier<
Size> -
getter/setter pair
- I → RxNet
-
final
Static Methods
-
create(
) → RxNet -
delete<
T> ({String? path}) → BuildRequest< T> -
get<
T> ({String? path}) → BuildRequest< T> -
getDefaultAdapter(
) → NetworkAdapter? -
getDefaultDatabase(
) → RxNetDataBase? -
getGlobalHeaders(
) → Map< String, dynamic> -
head<
T> ({String? path}) → BuildRequest< T> -
init(
{required RxNetConfig config}) → Future< void> - 使用 RxNetConfig 初始化(推荐方式)
-
options<
T> ({String? path}) → BuildRequest< T> -
patch<
T> ({String? path}) → BuildRequest< T> -
post<
T> ({String? path}) → BuildRequest< T> -
put<
T> ({String? path}) → BuildRequest< T> -
readCache<
T> (String key) → Future< T?> -
saveCache(
String key, dynamic value) → Future< void> -
setDefaultEnv(
String env) → void -
setGlobalHeaders(
Map< String, dynamic> header) → void -
showDebugWindow(
BuildContext context) → void -
zipRequest(
List< ZipRequest> requests, {bool eagerError = true, CancelToken? cancelToken, Duration? timeout}) → Future<ZipResults> - 并发执行多个基于回调的请求并返回聚合结果。 Executes multiple callback-based requests concurrently and returns aggregated results.