Ing Flutter, yen sampeyan pengin nindakake tumindak tartamtu sawise tartamtu timeout, sampeyan bisa nggunakake Future.delayed
fungsi bebarengan karo async
lan await
keywords. Iki contone:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Timeout Example'),
),
body: Center(
child: ElevatedButton(
onPressed:() {
performActionWithTimeout();
},
child: Text('Perform Action with Timeout'),
),
),
);
}
Future<void> performActionWithTimeout() async {
print('Action started');
// Simulate a delay of 3 seconds
await Future.delayed(Duration(seconds: 3));
print('Action completed after timeout');
}
}
Ing conto iki, nalika tombol ditekan, performActionWithTimeout
fungsi kasebut diarani. Ing fungsi iki, kita nggunakake await Future.delayed(Duration(seconds: 3))
wektu tundha 3 detik. Sawise wektu tundha, tumindak wis rampung.
Sampeyan bisa ngganti tumindak ing performActionWithTimeout
fungsi kasebut kanthi operasi sing dikarepake. Mekanisme iki timeout bisa mbiyantu nalika sampeyan pengin tundha tumindak tanpa ngalangi utas UI.
Elinga yen ana nganyari utawa paket anyar sing ana gandhengane timeout sawise Flutter nganyari pungkasan, sampeyan bisa uga pengin njelajah opsi kasebut.