InflightRequestManager

Version GitHub license

A Flutter package to manage in-flight asynchronous requests efficiently.

Usage

InFlightRequestManager is quite simple to use, just instantiate one and you can use it right away with any number of functions. It manages in-flight requests to prevent duplicate operations identified by unique IDs. If a request with the same ID is already in progress, calling it again can either return the previous call's future (in case it did not finish yet) or override it with a new request based on the override flag (which will override the result for previous unfinished calls as well).

This is particularly useful in scenarios like network requests where multiple requests for the same resource might be initiated consecutively.

Since it is using IDs, you don't necessarily have to use an ID with only one kind of request but generally that use-case is the more common.

Future<void> main() async {
  final manager = InFlightRequestManager<String>();

  Future<String> fetchData(int count, {bool override = false}) {
    return manager.run('api-call', () async {
      await Future.delayed(Duration(seconds: 2));
      return 'Data for $count. call';
    }, override: override);
  }

  final future1 = fetchData(1);
  final future2 = fetchData(2);
  final future3 = fetchData(3, override: true);

  print(await future1);
  print(await future2);
  print(await future3);
}

Parameters

Parameter Description
id ID of the request group.
task The actual callback that will be called
override Whether to override existing operations if there are any or not.

License

Copyright 2025 Norbert Csörgő

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.