asyncstate 3.0.0-dev.1 copy "asyncstate: ^3.0.0-dev.1" to clipboard
asyncstate: ^3.0.0-dev.1 copied to clipboard

This package will help you to call a loading in your async methods very easily, and you don't need to worried when it's is starting or finishing.

Async State

Platform


iOS Android Mac Linux Windows


Importante #

1 - The package uses the application's main navigator, so commands like "dart Navigator.of(context).pop()" can close the loader.
2 - The "Asyncvalue" class extends "ValueNotifier" and adds some extra functions, so the same precautions must be taken when using, such as calling "dispose".
3 - Don't worry if you call a "loade"r with another "loader" already open, the "asyncState" will just update the internal widget if necessary.
- 3.1 - This means there will be no impact from opening and closing, and you can pass a custom "loader" to change, indicating another type of wait.

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 "loader", or do something in "onError" function.

@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
      loader: const GlobalLoading(),
      onError: (error, stackTrace, context, routeSettings) {
        /// Here you can handle your exceptions
        switch (routeSettings?.name ?? '') {
          case '/Detail':
            DetailPageHandlerException.onError(
              error,
              stackTrace,
              context,
              routeSettings,
            );
            break;
          case _:
            ScaffoldMessenger.of(context!).showSnackBar(
              SnackBar(
                content: Text(
                  error.toString(),
                ),
              ),
            );
            break;
        }
      },
      builder: (navigatorObserver) => MaterialApp(
        navigatorObservers: [navigatorObserver],
        themeMode: ThemeMode.dark,
        theme: ThemeData.dark(),
        initialRoute: '/',
        routes: {
          '/': (context) => const HomeLoaderPage(),
          '/Detail': (context) => const DetailPage(),
          '/Profile': (context) => const ProfilePage()
        },
      ),
    );
  }

Method - (Extension) asyncLoader #

Use the “asyncLoader” in asynchronous calls, you won't have to worry about opening or closing the “loader”. #

await Future.delayed(const Duration(seconds: 2)).asyncLoader();

///Personalized
await Future.delayed(const Duration(seconds: 2)).asyncLoader(
  loader: const GlobalCustomLoading(),
);

Method - (Extension) asyncLazyLoader #

Use the “asyncLazyLoader” in asynchronous calls, it will maintain your loader opened so you can do something. #

await Future.delayed(const Duration(seconds: 2)).asyncLazyLoader();

// You can close by calling "AsyncLoader.hide()".
// Or calling "asyncLoader" in another function, and it will take care of closing.

await AsyncLoader.hide();
//OR
await Future.delayed(const Duration(seconds: 2)).asyncLoader();

Method - (Extension) asyncAwaitLoader #

Use the “asyncAwaitLoader” in asynchronous calls, it will call the loader only after the future was solved. #

 await Navigator.of(context).pushNamed('/Detail').asyncAwaitLoader();
// Here, we open the loader when we return from a page, to process the data received.
debugPrint('Detail Page Closed - Do something here');
await Future.delayed(const Duration(seconds: 2));
await AsyncLoader.hide();

Function - OnError #

The "onError" method, in "AsyncStateBuilder", allows you to handle application errors, generic or via routes, with access to parameters such as context, stackTrace, error and routeSettings. #

onError: (error, stackTrace, context, routeSettings) {
        /// Here you can handle your exceptions
        switch (routeSettings?.name ?? '') {
          case '/Detail':
            DetailPageHandlerException.onError(
              error,
              stackTrace,
              context,
              routeSettings,
            );
            break;
          case _:
            ScaffoldMessenger.of(context!).showSnackBar(
              SnackBar(
                content: Text(
                  error.toString(),
                ),
              ),
            );
            break;
        }
      },

Handler - AsyncLoader (Show or Hide) #

You can use "AsyncLoader.show()" and "AsyncLoader.hide()" to control the loader dynamically. #

await AsyncLoader.show();
await Future.delayed(const Duration(seconds: 2)).asyncLazyLoader();
await AsyncLoader.hide();

Type - AsyncValue #

"AsyncValue" is a class that extends "ValueNotifier" and adds some functions to help you, additionally methods for state control such as "Success", "Loading" and "Error", and shortcuts for quick definition.

All methods like "setSuccess", "setError", "setLoading" e "refresh" call "notifyListener" automatically.
  final _userModel = AsyncValue<UserModel?>(null);
  final _isLoading = false.asyncValue();

// Method like
_isLoading.setSuccess(false);
_isLoading.setLoading();
_isLoading.setError(error: MyException(), stackTrace: MyStackTrace());
//or
_userModel.setSuccess(UserModel(name: 'Leonardo Serrano'));

// And ".build" to UI
_userModel.build(
 onSuccess: (user) => Text('User: ${user?.name}'),
 onLoading: () => const CircularProgressIndicator(),
 onError: (error, stackTrace) => Text('Error: $error'),
),

// Or change the value using the ".value" and call ".refresh()".
_isLoading.value = false;
_isLoading.refresh();

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

51
likes
140
pub points
88%
popularity

Publisher

verified publisherleonardoserrano.dev

This package will help you to call a loading in your async methods very easily, and you don't need to worried when it's is starting or finishing.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on asyncstate