Your Flutter screen fetches on every build. It should fetch once.
A screen I reviewed called the API inside build. Every rebuild fired the request again, so a keyboard opening, a theme change, or any setState on the screen sent the same call to the server. The list flickered, the loading spinner came back, and the server saw traffic the user never asked for.
What actually happened
The screen showed a list of orders. The fetch lived inside build, right where the list was described. It looked harmless, because the first time the screen opened, it worked. One request, one list, no visible problem.
Then the user tapped the search field. The keyboard opened, the screen size changed, and build ran again. The fetch ran again. The search field called setState on every keystroke. Another rebuild, another request. A theme change, a parent widget updating, each one sent the same call to the server again.
The loading spinner came back on every rebuild, the list flickered, and the server log filled with duplicate requests for data the screen already had. The user never asked for any of it. The screen asked on its own, because build ran again and the fetch was sitting inside it.
The wrong fix
The wrong fix is to add a cache in front of the call, or to block rebuilds.
A cache hides the real problem. The screen still fetches in the wrong place, it just fetches from a local copy instead of the server. The moment the cache expires, the duplicate requests come back.
Blocking rebuilds is worse. Rebuilds are how Flutter updates the screen. Fighting them means fighting the framework, and the screen stops responding to real changes. Both fixes treat the symptom. The problem is that a fetch is work with a result, and build is only meant to describe what the screen shows.
The real fix: fetch once, build reads the result
Fetch once when the screen starts, store the future, and let build only read the result:
class OrdersScreen extends StatefulWidget {
const OrdersScreen({super.key});
@override
State<OrdersScreen> createState() => _OrdersScreenState();
}
class _OrdersScreenState extends State<OrdersScreen> {
late Future<List<Order>> _ordersFuture;
@override
void initState() {
super.initState();
_ordersFuture = fetchOrders();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Order>>(
future: _ordersFuture,
builder: (context, snapshot) {
if (snapshot.hasError) return const ErrorView();
if (!snapshot.hasData) return const LoadingView();
final orders = snapshot.data!;
if (orders.isEmpty) return const EmptyView();
return OrderList(orders: orders);
},
);
}
}
One call, one result, four states in build: loading, error, empty, and data. The screen rebuilds as often as it likes and the server hears nothing new.
The key detail is where the future is created. It is created once, in initState, and stored in a field. Build never creates it, it only passes the same stored future to FutureBuilder. Because FutureBuilder receives the same future on every rebuild, it keeps showing that future's result instead of starting a new request.
Don't skip the empty state. An API that returns an empty list is a success, so hasData is true, and without the check the user sees a blank screen with no explanation.
Why build must stay free of work
Build runs whenever Flutter decides the screen needs to be described again. You do not control how often that happens, and you should not try to. A parent widget updates, a theme changes, a keyboard opens, and build runs. That is normal, and it is cheap, because build only creates widget descriptions.
The moment build starts real work, that cheap operation turns expensive. A network call, a database read, a heavy calculation, each one runs again on every rebuild, and none of them belong there. Build should read state that already exists. The work that creates that state belongs in initState, in an event handler, or in a state management layer, never in build.
A simple way to check your own screens: look at build and ask what would happen if it ran ten times in a row. If the answer is ten network requests, the fetch is in the wrong place.
The fetch itself
The fetch is a plain async function. It returns a list of orders and it does nothing else:
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<List<Order>> fetchOrders() async {
final response = await http.get(
Uri.parse('https://api.example.com/orders'),
);
if (response.statusCode != 200) {
throw Exception('Failed to load orders.');
}
final List<dynamic> jsonList = jsonDecode(response.body);
return jsonList.map((json) => Order.fromJson(json)).toList();
}
Keeping the fetch in its own function matters. The screen does not know how orders are loaded. It only knows it has a future and four states to show. If the endpoint changes, only this function changes.
What about refreshing the list?
Fetching once does not mean the list can never update. It means the screen does not fetch by accident. When the user asks for fresh data, fetch again on purpose, in an event handler:
Future<void> _refreshOrders() {
final future = fetchOrders();
setState(() {
_ordersFuture = future;
});
return future;
}
This is why the field is late, not late final: a refresh assigns a new future, and a late final field can only be assigned once.
Returning the future matters too. Pass this method to a RefreshIndicator's onRefresh, and the refresh spinner stays visible until the new data arrives. A refresh button can call the same method.
The user asked for new data, so a new request is correct. The difference is intent. A rebuild is not a request for fresh data. A refresh is.
If the screen takes a parameter, like an order or customer id, also fetch again in didUpdateWidget when that parameter changes. initState runs only once per state object.
A checklist for your own screens
Three quick checks catch this mistake anywhere in your app.
First, search your widgets for network calls inside build. Any call that starts with http, a repository method, or a database query inside build is a fetch in the wrong place.
Second, check that every FutureBuilder receives a future stored in a field, created in initState. If the future is created inline in build, it is recreated on every rebuild, and the fetch runs again.
Third, watch the network tab while you use the screen: open the keyboard, switch the theme, interact with anything that calls setState. If the same request repeats without the user asking for new data, the screen is fetching on rebuild.
Where does your fetch live today, in build or in initState? Drop it in the comments.
I'm Sohail Khan, a Senior Full Stack Developer building AI agents and automation. I write about backend engineering, AI integration and practical system design.
This article was written with AI assistance and reviewed by me.
Top comments (0)