api_result_kit 1.0.0
api_result_kit: ^1.0.0 copied to clipboard
A typed Result/Error handling kit for Flutter apps. Normalizes errors from FastAPI, Express, and generic REST backends into a consistent ApiResult<T> type, with ready-made widgets for consistent UI er [...]
Changelog #
1.0.0 #
Initial release.
Data layer #
ApiResult<T>— a sealedApiSuccess<T>/ApiFailure<T>result type, withwhen,maybeWhen,map,flatMap,tap,dataOrNull, anderrorOrNull. No code generation, nofreezeddependency.ApiException— a normalized error shape withtype,message,statusCode,fieldErrors,raw,retryable, and asuggestedDisplayrecommendation for how to surface it in the UI.ApiErrorType— the seven error categories every failure is bucketed into:network,auth,validation,notFound,server,cancelled,unknown.ApiErrorParser— converts any caught error, including aDioException, into a normalizedApiException. Understands FastAPI'sdetailshape and Express'smessage/errorsshapes out of the box. Extensible viaaddMessageParserandaddFieldErrorParserfor other backend conventions.
Calling your API #
Dio.safeRequest— wraps a single Dio call, returning anApiResult<T>instead of throwing.Dio.safeRequestWithRetry— same as above, with automatic retry according to aRetryPolicy.RetryPolicy— configurablemaxAttempts,initialDelay,backoffFactor, and an optionalretryIfpredicate. Ships with aRetryPolicy.nonepreset.SafeDioInterceptor— attach once to aDioinstance to get automatic retry and error normalization on every request made through it, with no per-call-site code required. ExposesonApiErrorandonRetryhooks.
Automatic error UI #
ApiResultKitNavigator— a global navigator key so non-widget code (like the interceptor) can reach aBuildContext.showDefaultErrorSnackbar— a ready-madeonApiErrorcallback that shows a themedRichSnackbarautomatically, with sensible skip rules for validation and cancelled errors.withDefaultErrorSnackbar— composes the default snackbar with a custom callback, for example to redirect to a login screen on an auth failure.ApiErrorHandler— aBuildContext-based alternative for screens that already have a context in hand, with a configurableonAuthFailureredirect.Dio.safeRequestHandled— likesafeRequest, but shows the appropriate error UI automatically viaApiErrorHandler.
Widgets #
ApiResultBuilder<T>— declaratively renders the success or failure branch of anApiResult<T>.DefaultErrorWidget— a theme-aware fallback error view used byApiResultBuilder, with an icon per error type, a field-error summary, and a conditional retry button.RichSnackbar— anOverlay-based snackbar system with top/bottom placement, four layouts (floatingSmall,floatingLarge,bannerSmall,bannerLarge), four entrance/exit animations, and icon, asset, network, or SVG leading visuals. Snackbars queue independently per screen edge and return aRichSnackbarHandlefor early dismissal.
Testing #
- Full test coverage for
ApiResult,ApiException,ApiErrorParser,RetryPolicy, andSafeDioInterceptor, including every supported error shape, retry/backoff behavior, and edge cases around custom parser registration.