异步状态
适用于 Flutter 的软件包
- 星标
- 8
- 复刻
- 2
- 最近推送(UTC)
- 2025年5月8日
- 项目状态
- 未归档
原始 README
以下为英文项目原文快照,最新内容请访问 GitHub。
Async State
Usage
1 - Wrap your MaterialApp Or CurpertinoApp with the AsyncStateBuilder.2 - Get the "navigatorObserver" from the builder function and add it to your component's "navigatorObservers".
3 - If you want, you can add a widget to "CustomLoader", exceptionHandlers or disable the log.
Code example:
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
/// Here you need to wrap your MaterialApp with the AsyncStateBuilder
return AsyncStateBuilder(
/// Here you can customize your default loading that will show every transaction
/// Leave it and it will show a simple CircularProgress.adaptive indicator
customLoader: const GlobalLoading(),
/// Here you can enable or disable the log
enableLog: true,
///You can customize your exceptions handlers for route
exceptionHandlers: {
'_': GlobalExceptionHandler(),
'/Home/Detail': DetailExceptionHandler(),
},
builder: (navigatorObserver) => MaterialApp(
themeMode: ThemeMode.dark,
theme: ThemeData.dark(),
/// Here you need to pass the navigatorObserver to the MaterialApp
navigatorObservers: [navigatorObserver],
initialRoute: '/Home',
routes: {
'/Home': (context) => const HomePage(),
'/Home/Detail': (context) => const DetailPage(),
'/Home/Detail/SecondDetail': (context) => const SecondDetailPage(),
},
),
);
}
}
Attention
All methods allow you to customize the “loader”, when you change the "enum LoaderType", you need to ensure that the "customLoader" is already a widget of the same type.
Method - (Extension) asyncState
Use the “asyncState” extension in asynchronous calls, you won't have to worry about opening or closing the “loader”.
Future<void> loginSuccess() async {
final result = await _functionSuccess().asyncLoader();
debugPrint('Login Success result: $result');
}
///Personalized
Future<void> loadMorePersonalized() async {
await _functionFailure().asyncLoader(
customLoader: HomeCustomLoaderSnackbar(),
loaderType: LoaderType.snackBar,
);
}
Method - (Class OR Context Extension) AsyncLoaderHandler
The "AsyncLoaderHandler" method, allows you to have control over the open loader. Don't worry about “exceptions”, "asyncState" will close the loader if an “exception” is raised before you call “close”.
Future<void> loginSuccessHandler() async {
final handler = AsyncLoaderHandler.start();
final result = await _functionSuccess().asyncLoader();
debugPrint('Login Success result: $result');
handler.close();
}
///OR On View
ElevatedButton(
onPressed: () async {
context.startAsyncStateLoader();
await errorCall();
context.closeAsyncStateLoader();
},
child: const Text('Loader Error by context'),
),
Method - (Interface Class) AsyncStateExceptionHandler
You can create a class that extends "AsyncStateExceptionHandler", with it, you can handle “exceptions” automatically and per route.
It is necessary to check the "exception.runtimeType", or it will execute the action in any “exception”.
class DetailExceptionHandler implements AsyncStateExceptionHandler {
@override
void onException(
Object exception,
StackTrace stackTrace,
BuildContext context,
) {
switch (exception.runtimeType) {
case DetailException:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
exception.toString(),
),
),
);
case _:
log(exception.toString());
break;
}
}
}
On Main
In “main”, you can define a specific “AsyncStateExceptionHandler” for each route. The “_” is global, it will be used if the current route does not have a unique “AsyncStateExceptionHandler”.
///You can customize your exceptions handlers for route
exceptionHandlers: {
'_': GlobalExceptionHandler(),
'/Home/Detail': DetailExceptionHandler(),
},
Bugs or Requests
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on GitHub and I'll look into it. Pull request are also welcome.
Contributors
Leonardo Serrano |
Marcus Brasizza |
Felipe Sales |
If you like what I do, maybe consider buying me a coffee/tea 🥺👉👈
语言DartC++CMakeSwiftCHTMLKotlinObjective-C
使用的依赖
依赖清单 3 项
- flutter
{"sdk":"flutter"} - flutter_test开发依赖
{"sdk":"flutter"} - flutter_lints开发依赖
^5.0.0
如何选择适合项目的代码仓?
先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。
- 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
- 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
- 使用前在 GitHub 核对 README、许可证与 Issues。
来源与维护
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗
