Problem Statement
What is FutureBuilder used for in Flutter?
Explanation
FutureBuilder is a widget that rebuilds based on the state of a Future, automatically showing different UI for loading, success, and error states. It takes a Future and builder function receiving context and AsyncSnapshot, which contains connection state (none, waiting, active, done) and data or error. FutureBuilder handles the lifecycle of listening to the Future and rebuilding when it completes.
The builder function checks snapshot.connectionState to determine what to show - typically showing loading indicator when waiting, error message if snapshot.hasError, and actual data when done and snapshot.hasData. This pattern eliminates manual state management for simple async data loading, making it perfect for fetching and displaying data from APIs or databases.
FutureBuilder should receive the same Future instance across rebuilds - don't create new Futures in build method as this causes issues. Store the Future in a variable or make it final in State. For repeatedly fetching data, use streams with StreamBuilder instead. FutureBuilder is ideal for one-time data fetching operations like loading user profile or fetching configuration on screen load.
