Fresh Http 是一个基于 `http` 包构建的 Flutter/Dart 包,提供自动刷新的 HTTP 客户端。它能自动处理认证令牌的刷新,确保在无需手动管理令牌的情况下实现无缝 API 调用。对于需要安全、持久访问已认证 API 的应用来说,该客户端通过透明刷新过期令牌来减少样板代码并提高可靠性。该客户端可与现有 HTTP 工作流程无缝集成,并支持所有主要平台,包括 Web、移动设备和桌面。
适合场景
需要刷新令牌的 API
安全的移动和Web应用程序
减少身份验证样板代码
自动令牌管理
有状态的 API 客户端
主要特点
自动令牌刷新
基于 `package:http`
透明的身份验证处理
跨平台支持
所需设置最少
适合对象
使用 JWT 或 OAuth2 的应用
寻求降低认证复杂性的开发者
需要可靠 API 连接的项目
重视开发者体验的团队
频繁进行 API 交互的应用程序
使用建议
需要自定义令牌刷新逻辑
依赖外部身份验证状态管理
仅限基于 HTTP 的 API
不适用于一次性请求
令牌刷新流程中需要适当的错误处理
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Fresh 🍋
A collection of packages for automatic token refresh in Dart and Flutter. Fresh handles refreshing, caching, and attaching authentication tokens transparently so your API calls just work.
Why Fresh?
Token-based authentication seems simple until you handle the edge cases: tokens expire mid-session, multiple requests fail at the same time triggering duplicate refreshes, refresh tokens get revoked, and you need to route users to login when auth is lost. Fresh handles all of this so you don't have to.
No more manual 401 handling scattered across your codebase
No more race conditions when concurrent requests trigger simultaneous refreshes
No more stale tokens causing request failures - expired tokens are refreshed proactively before the request is even sent