Skip to content

sinianbao/fnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FNet

A powerful and easy-to-use Flutter network library based on Dio.

Features

  • HTTP/2 Support - Improved performance with multiplexing and header compression
  • Internationalization (i18n) - Customizable error messages for any language
  • Automatic Retry - Smart retry mechanism for failed requests
  • Response Caching - Configurable cache strategy with memory/disk storage
  • Token Refresh - Automatic 401 handling with token refresh and request retry
  • Loading Indicator - Built-in loading state management
  • Error Handling - Comprehensive network error handling with toast support
  • Request Logging - Beautiful request/response logging with PrettyDioLogger

Installation

Add this to your pubspec.yaml:

dependencies:
  fnet:
    git:
      url: https://github.com/your-repo/fnet.git

Quick Start

Basic Configuration

import 'package:fnet/fnet.dart';

void main() {
  NetOptions.instance
    .setBaseUrl("https://api.example.com/")
    .setConnectTimeout(const Duration(seconds: 30))
    .setReceiveTimeout(const Duration(seconds: 30))
    .addHeaders({"Content-Type": "application/json"})
    .enableLogger(true)
    .create();
}

Enable HTTP/2

NetOptions.instance
  .setBaseUrl("https://api.example.com/")
  .enableHttp2(true)  // Enable HTTP/2
  .create();

Enable Retry

NetOptions.instance
  .setBaseUrl("https://api.example.com/")
  .enableRetry(
    enable: true,
    retryCount: 3,
    retryDelay: const Duration(seconds: 1),
  )
  .create();

Enable Caching

NetOptions.instance
  .setBaseUrl("https://api.example.com/")
  .enableCache(
    enable: true,
    cacheOptions: CacheOptions(
      store: MemCacheStore(),
      policy: CachePolicy.request,
    ),
  )
  .create();

Custom Localizations (i18n)

class EnglishLocalizations implements NetLocalizations {
  @override
  String get noNetworkConnection => 'No network connection';
  @override
  String get connectionTimeout => 'Connection timeout';
  // ... implement other methods
}

NetOptions.instance
  .setLocalizations(EnglishLocalizations())
  .create();

Making Requests

// GET request
final result = await get<User, User>(
  'users/1',
  fromJsonFunc: User.fromJson,
  isShowLoading: true,
);

if (result.isSuccess) {
  print(result.data);
} else {
  print(result.msg);
}

// POST request
final result = await post<void, Map>(
  'users',
  data: {'name': 'John', 'email': 'john@example.com'},
);

Response Parsing: fromJsonFunc vs converter

Feature fromJsonFunc converter
Control Low (Model only) High (Entire Response)
Use Case Standard JSON Non-standard / Raw Data

1. Using fromJsonFunc (Standard) Most common. Parses the data field using your model's fromJson method.

get<User, User>(
  'users/1',
  fromJsonFunc: User.fromJson,
);

2. Using converter (Custom) Full control over the Response. Bypasses default decoding.

get<String, String>(
  'raw-text',
  converter: (response) {
    return response.data.toString();
  },
);

3. Returning a List The library automatically handles lists. fromJsonFunc will be called for each item.

// Result<List<User>>
get<User, List<User>>(
  'users',
  fromJsonFunc: User.fromJson, // Parses ONE item
);

Token Refresh Interceptor

NetOptions.instance
  .addInterceptor(TokenRefreshInterceptor(
    dio: NetOptions.instance.dio,
    getToken: () => myTokenStorage.accessToken,
    onRefresh: () async {
      final newToken = await refreshTokenAPI();
      myTokenStorage.accessToken = newToken;
      return newToken;
    },
  ))
  .create();

Dynamic Headers

NetOptions.instance
  .addInterceptor(HeadersInterceptor(
    staticHeaders: {'X-App-Version': '1.0.0'},
    dynamicHeaders: {
      'Authorization': () => 'Bearer ${getToken()}',
    },
  ))
  .create();

API Reference

NetOptions Methods

Method Description
setBaseUrl(String) Set the base URL for all requests
setConnectTimeout(Duration) Set connection timeout
setReceiveTimeout(Duration) Set receive timeout
setSendTimeout(Duration) Set send timeout
addHeaders(Map) Add default headers
addInterceptor(Interceptor) Add a custom interceptor
enableLogger(bool) Enable/disable request logging
enableHttp2(bool) Enable/disable HTTP/2
enableRetry({...}) Configure automatic retry
enableCache({...}) Configure response caching
setLocalizations(NetLocalizations) Set custom error messages
setShowLoadingFunc(VoidCallback) Set loading show callback
setDismissLoadingFunc(VoidCallback) Set loading dismiss callback
setShowToastFunc(ToastCallback) Set toast callback
create() Build and apply configuration

Request Methods

  • get<T, K>(...) - HTTP GET
  • post<T, K>(...) - HTTP POST
  • put<T, K>(...) - HTTP PUT
  • delete<T, K>(...) - HTTP DELETE
  • patch<T, K>(...) - HTTP PATCH
  • head<T, K>(...) - HTTP HEAD
  • download(...) - Download file

Dependencies

License

MIT License

About

flutter 基于dio网络框架封装

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages