value_state 2.0.1 copy "value_state: ^2.0.1" to clipboard
value_state: ^2.0.1 copied to clipboard

This Dart package simplifies the implementation of common data-fetching states (loading, success, error).

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:value_state/value_state.dart';

import 'widgets/action_button.dart';
import 'widgets/app_root.dart';
import 'widgets/counter_notifier.dart';
import 'widgets/default_error.dart';
import 'widgets/formatted_column.dart';
import 'widgets/loader.dart';

// coverage:ignore-start
void main() {
  runApp(const MyApp());
}
// coverage:ignore-end

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) =>
      CounterNotifier(child: const AppRoot(child: MyHomePage()));
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) =>
      // This example, show how to handle different states with refetching
      // problematic. In this case, when an error is raised after a value has
      // been successfully fetched, we can see the error and the last value
      // fetched both displayed.
      ValueListenableBuilder(
        valueListenable: CounterNotifier.of(context),
        builder: (context, state, _) {
          if (state.isInitial) return const Loader();

          return FormattedColumn(children: [
            RefreshLoader(isLoading: state.isRefetching),
            if (state case Value(:final error?)) DefaultError(error: error),
            if (state case Value(:final data?)) Text('Counter value : $data'),
            ActionButton(
              onPressed: state.isRefetching
                  ? null
                  : CounterNotifier.of(context).increment,
            ),
          ]);
        },
      );
}
5
likes
160
points
2.15k
downloads

Publisher

verified publisherdevobs.dev

Weekly Downloads

This Dart package simplifies the implementation of common data-fetching states (loading, success, error).

Homepage
Repository (GitHub)

Topics

#error #fetch #init #state #success

Documentation

API reference

License

MIT (license)

Dependencies

meta, stream_transform

More

Packages that depend on value_state