When is ConnectionState.waiting true?
ConnectionState.waiting means the Future has not completed yet.
Build UI based on async operations
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
FutureBuilder is a widget that listens to a Future and automatically rebuilds the UI whenever the state of that Future changes. It tracks three main phases: loading (the Future is still running), success (the Future completed with data), and error (the Future threw an exception). This means you do not need to manually call setState or track loading flags yourself - FutureBuilder handles all of that for you.
1FutureBuilder<User>(2future: fetchUser(), // The async operation3builder: (context, snapshot) {4// Loading state5if (snapshot.connectionState == ConnectionState.waiting) {6return CircularProgressIndicator();7}89// Error state10if (snapshot.hasError) {11return Text('Error: ${snapshot.error}');12}1314// Success state15if (snapshot.hasData) {16final user = snapshot.data!;17return Text('Hello, ${user.name}');18}1920// No data yet21return Text('No data');22},23)
Here is a full example that fetches a user from an API and displays it. Notice how the Future is stored in initState so it only runs once, not on every rebuild. The builder uses a switch statement to handle every possible connection state.
1class UserProfile extends StatefulWidget {2@override3_UserProfileState createState() => _UserProfileState();4}56class _UserProfileState extends State<UserProfile> {7late Future<User> _userFuture;89@override10void initState() {11super.initState();12_userFuture = fetchUser(); // Call once in initState13}1415@override16Widget build(BuildContext context) {17return Scaffold(18appBar: AppBar(title: Text('Profile')),19body: FutureBuilder<User>(20future: _userFuture, // Use stored future21builder: (context, snapshot) {22switch (snapshot.connectionState) {23case ConnectionState.none:24return SizedBox.shrink();25case ConnectionState.active:26case ConnectionState.waiting:27return Center(child: CircularProgressIndicator());28case ConnectionState.done:29if (snapshot.hasError) {30return Center(child: Text('Something went wrong'));31}32return UserCard(user: snapshot.data!);33}34},35),36);37}38}
Never call the async function directly in the future parameter. If you write future: fetchUser(), a brand new Future is created on every rebuild, causing infinite API calls. Always store the Future in a variable inside initState and pass that variable to the future parameter instead.
Treat FutureBuilder screens as state machines:
This keeps async UX predictable and easy to test.
Build UI based on async operations. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Connect each concept to real app features you can build.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
ConnectionState.waiting means the Future has not completed yet.
Store the Future in initState to prevent multiple calls on rebuild.
hasData returns true when the Future has completed successfully with a non-null result.
FutureBuilder<T> expects a Future<T> parameter matching its type parameter.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue