パッケージ、リポジトリ、アプリカテゴリを検索

GitHub リポジトリlleoserrano/asyncstate

非同期状態

Flutter用パッケージ

非同期状態 のプロジェクト画像
スター
8
フォーク
2
最終プッシュ(UTC)
2025/05/08
プロジェクト状態
公開中

元の README

以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。

Async State

Platform


iOS Android Mac Linux Windows


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 🥺👉👈

Buy Me A Coffee

Leonardo Serrano GitHub avatar
GitHub 作者

Leonardo Serrano ↗

Flutter Dev | Exploring Golang | Clean Code • Modular Architecture • Docker Curious

言語DartC++CMakeSwiftCHTMLKotlinObjective-C

使用している依存関係

依存関係一覧 3 件

  • flutter{"sdk":"flutter"}
  • flutter_test開発用{"sdk":"flutter"}
  • flutter_lints開発用^5.0.0

プロジェクトに合うリポジトリを選ぶには?

用途で絞り込み、開発状況を比較し、採用前に元のリポジトリで要件を確認しましょう。

  1. 名前・作者・説明で検索し、技術トピックで絞り込みます。
  2. スター、フォーク、最近のプッシュを比較します。スターは関心の目安であり、品質の保証ではありません。
  3. 利用前に GitHub で README、ライセンス、Issues を確認します。

情報源と管理

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗