패키지, 저장소 및 앱 카테고리 검색

GitHub 저장소lleoserrano/asyncstate

비동기 상태

Flutter용 패키지

비동기 상태 프로젝트 이미지
스타
8
포크
2
최근 푸시(UTC)
2025. 5. 8.
프로젝트 상태
활성

원본 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 ↗