2
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
julemand101
  • 28,470
  • 5
  • 52
  • 48

2 Answers2

1

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

0

Since Future.duration doesn't

  await Future.delayed(const Duration(seconds: 1));

credit: How can I "sleep" a Dart program

ethicnology
  • 474
  • 5
  • 13