Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error: Unsupported operation: ProcessUtils._sleep

Tags:

dart

import 'dart:io';

void main() {
 performTask();
}

void performTask() {
 task1();
 task2();
 task3();
}

void task1() {
 print('task1');
}

void task2() {
 Duration timeDuration = Duration(seconds: 3);
 sleep(timeDuration);
 print('task2');
}

void task3() {
 print('task3');
}

After executing first function that is task1() it throws an error:

Uncaught Error: Unsupported operation: ProcessUtils._sleep
like image 297
Firdaus Siddiqui Avatar asked Feb 14 '26 22:02

Firdaus Siddiqui


1 Answers

I just hit the same roadblock! Not sure how to get sleep to work, but i found that using async/await is a bit more predictable:

// Unused import
// import 'dart:io'; // Delete me

void main() {
 performTask();
}

// No need for async/await here, just the method in which it's used to await Future.delayed
void performTask() {
 task1();
 task2();
 task3();
}

void task1() {
 print('task1');
}

// I'm still a bit new to flutter, but as I understand it, best practice is to use Future<T> myFunction() {...} when defining async/await method.
// In this case <T> is <void> because you're not returning anything!
Future<void> task2() async {
 Duration timeDuration = Duration(seconds: 3);
 // sleep(timeDuration) // Delete Me
 await Future.duration(timeDuration); // replacement for the sleep method, comes from the 'package:flutter/material.dart'
 print('task2');
}

void task3() {
 print('task3');
}

Credit: How can I "sleep" a Dart program

like image 93
Nicholas Stine Avatar answered Feb 21 '26 15:02

Nicholas Stine