非同步狀態
適用於 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 ↗
