Search packages, repositories and app categories

Package detailsv2.0.0

enhanced_future_builder

Wrapper that improves readability and solves the common refiring FutureBuilder problem

AndroidiOSWebmacOSWindowsLinux
30-day downloads
1.1K
Likes
29
Pub points
140

Install package

flutter pub add enhanced_future_builder

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

EnhancedFutureBuilder is a Flutter package that enhances the standard FutureBuilder by improving code readability and eliminating the common issue of unnecessary rebuilds when the future completes. It provides a cleaner, more intuitive API for handling asynchronous data, reducing boilerplate and preventing unintended widget rebuilds. Built as an extension to Future, it integrates seamlessly into existing Flutter projects. The package supports all major platforms including Android, iOS, Web, macOS, Windows, and Linux.

Use cases

  • Handling async data with reduced rebuilds
  • Improving code clarity in state management
  • Simplifying future-based UI updates
  • Replacing verbose FutureBuilder patterns
  • Streamlining loading/error/loading states

Key features

  • Prevents unwanted rebuilds after future completion
  • Cleaner, more readable syntax
  • Built-in support for loading, error, and data states
  • Extends Future for intuitive usage
  • Cross-platform compatibility

Suitable for

  • Developers seeking simpler async UI handling
  • Projects aiming to reduce widget rebuilds
  • Teams prioritizing code maintainability
  • Flutter beginners learning async patterns
  • Apps with frequent async data fetching

Considerations

  • Requires Dart 2.12 or higher
  • May require refactoring existing FutureBuilder code
  • Limited to use with Future-based operations
  • Not suitable for complex stream scenarios
  • Depends on proper future lifecycle management

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

This library is no longer maintained. Use at your own discretion.

enhanced_future_builder

Small FutureBuilder wrapper to improve readabiltity. It can also be used as an easy solution to the common 'my FutureBuilder keeps refiring' problem (more info about that here).

Actions Status codecov

Stop FutureBuilder from refiring

Let's say you want to build an app that displays a random cat from the internet at launch and then increases a counter whenever a button is clicked. You came up with the following code:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Clicked $_counter times')
    ),
    body: FutureBuilder(
      // resolves to cat data from the internet
      future: widget._api.getRandomCat(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          // builds an image widget containing a random cat
          return _showCatWidget(snapshot.data);
        }else {
          return Center(child: Text('Loading...'));
        }
      }
    ),
    floatingActionButton: FloatingActionButton(
      // _incrementCounter calls setState() to update the view
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    ),
  );
}

Which results in the following app:

I love cats

As you can see there's a problem. Whenever the button is clicked, a new cat is shown to the user. This is not what we want and can be solved by using EnhancedFutureBuilder. Import the package and change the code to:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Clicked $_counter times')
    ),
    body: EnhancedFutureBuilder(
      future: widget._api.getRandomCat(),
      // this is where the magic happens
      rememberFutureResult: true,
      whenDone: (dynamic cat) => _showCatWidget(cat),
      whenNotDone: Center(child: Text('Loading...')),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    ),
  );
}

As you can see the code is a little easier to read now and the result will be just how we want it to be:

yeah cats are great

Usage

Ironically, EnhancedFutureBuilder doesn't require a builder anymore.

FutureBuilder:

FutureBuilder(
  future: _futureToResolve(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      return MyWidget(snapshot.data);
    }else if (snapshot.connectionState == ConnectionState.waiting){
      return Center(child: Text('Waiting...'));
    }
    }else if (snapshot.connectionState == ConnectionState.active){
      return Center(child: Text('Active...'));
    }
    // ...
  }
}

EnhancedFutureBuilder:

EnhancedFutureBuilder(
  future: _futureToResolve(),
  rememberFutureResult: false,
  whenDone: (dynamic data) => MyWidget(data),
  whenWaiting: Center(child: Text('Waiting...')),
  whenActive: Center(child: Text('Active...')),
  // whenNone: ...
),

Categories

Source repository

Projects using this package

No data available yet.