Future re-run
- Flutter course
- Intermediate
- Lesson 24/99
- 4 min
Use key and setState to re-run a Future.
Rerunning the Future
To rerun a FutureBuilder, you need to give it a new Future instance. By reassigning the _future variable inside setState, the FutureBuilder is rebuilt and the Future runs again.
void retry() {
setState(() {
_future = fetchData();
});
}A new Future assignment retriggers the FutureBuilder.
key and setState
Using a key is another way to recreate the FutureBuilder. When the ValueKey changes on each attempt, the widget is rebuilt. But the most common method is assigning a new Future with setState.
FutureBuilder(
key: ValueKey(_attemptCount),
future: _future,
builder: ...,
)When the key changes, the widget is fully recreated.
Practice Trigger Future with a retry button
The veriGetir async method should wait 100 ms and return 'Yeniden yüklendi'. In Ekrani stateful widget, hold a _future field, call print('ekran açıldı') in initState and assign veriGetir() to _future. On button press, reassign _future with setState. Display snapshot.data ?? 'bekle' with FutureBuilder<String>.
Starter code
import 'package:flutter/material.dart';
Future<String> veriGetir() async {
await Future.delayed(Duration(milliseconds: 100));
return 'Yeniden yüklendi';
}
class Ekrani extends StatefulWidget {
@override
_EkraniState createState() => _EkraniState();
}
class _EkraniState extends State<Ekrani> {
// _future field ekle
// initState: print çağır ve future'ı başlat
// yenidenDene: setState ile future'ı yeniden ata
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('bekle')),
floatingActionButton: FloatingActionButton(
onPressed: yenidenDene,
child: Icon(Icons.refresh),
),
);
}
}
void main() => runApp(MaterialApp(home: Ekrani()));Sample solution
import 'package:flutter/material.dart';
Future<String> veriGetir() async {
await Future.delayed(Duration(milliseconds: 100));
return 'Yeniden yüklendi';
}
class Ekrani extends StatefulWidget {
@override
_EkraniState createState() => _EkraniState();
}
class _EkraniState extends State<Ekrani> {
late Future<String> _future;
@override
void initState() {
super.initState();
print('ekran açıldı');
_future = veriGetir();
}
void yenidenDene() {
setState(() {
_future = veriGetir();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder<String>(
future: _future,
builder: (context, snapshot) {
return Text(snapshot.data ?? 'bekle');
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: yenidenDene,
child: Icon(Icons.refresh),
),
);
}
}
void main() => runApp(MaterialApp(home: Ekrani()));Expected output
ekran açıldı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the correct way to rerun a FutureBuilder?
- Giving the same Future reference again
- Assigning a new Future inside setState Correct answer
- Doing hot reload
- Closing and reopening the app
Explanation: Assigning a new Future instance to _future inside setState retriggers the FutureBuilder and reruns the Future.
2 Reassign the Future with setState
void yenidenDene() {
setState(() {
_future = ____();
});
}
void yenidenDene() {
setState(() {
_future = veriGetir();
});
}
Explanation: Inside setState, a new veriGetir() call is assigned to _future; this retriggers the FutureBuilder.
3 Future rerun parts
| Term | Meaning |
|---|---|
setState |
Block where reassignment happens |
yeni Future |
Assignment triggering FutureBuilder |
ValueKey |
Key to recreate the widget |
_future değişkeni |
State field being reassigned |