Flutter 应用程序不断处理异步操作(网络请求、文件访问、数据库查询)。Dart 的 Futures(单个异步结果)和 Streams(异步事件序列),结合 async/await 和 FutureBuilder/StreamBuilder 等 widgets,对异步 UI 至关重要。
Futures 和 async/await
// a Future represents a single async result that completes later
Future<String> fetchUser() async {
final response = await http.get(url); // await pauses until the Future completes
return response.body; // returns the result
}
// using it
final data = await fetchUser(); // await the result
// or: fetchUser().then((data) => ...); // callback style
