A cikin Flutter, idan kuna son aiwatar da wani takamaiman aiki bayan ƙayyadaddun ƙayyadaddun timeout, zaku iya amfani da Future.delayed
aikin tare da async
kalmomin await
shiga. Ga misali:
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');
}
}
A cikin wannan misali, lokacin da aka danna maɓallin, performActionWithTimeout
ana kiran aikin. A cikin wannan aikin, muna amfani await Future.delayed(Duration(seconds: 3))
da gabatar da jinkiri na 3 seconds. Bayan jinkiri, an kammala aikin.
Kuna iya maye gurbin aikin a cikin performActionWithTimeout
aikin tare da aikin da kuke so. Wannan timeout tsarin zai iya taimakawa lokacin da kake son jinkirta aiki ba tare da toshe zaren UI ba.
Ka tuna cewa idan an sami wasu sabuntawa ko sabbin fakiti masu alaƙa da su timeout bayan Flutter sabuntawa na ƙarshe, kuna iya bincika waɗannan zaɓuɓɓukan suma.