1

Recently I was trying out some code snippets from "Dart For Absolute Beginners" on DartPad. Specifically:

import 'dart:math';
import 'dart:io';

void main() 
{
    int guess;
    Random rand = new Random(); //create a random number generator
    int answer = rand.nextInt(100); //gets a random integer from 0 to 99
    
    do 
    {
        print("Enter your guess:");
        String temp = stdin.readLineSync(); //read in from the keyboard
        guess = int.parse(temp); //convert String to integer
        if (guess < answer)  
        {
            print("Too low!");
        } 
        else if (guess > answer)  
        {
            print("Too high!");
        }
    }
   
    while (guess != answer);
    
    print("You got it!");
}

However upon running, it shows the error "Error compiling to JavaScript: unsupported import: dart:io"

So my question is

Is it that we can't run I/O operations on DartPad and we need a full fledge editor for that ? or is there something else wrong ??

Jay
  • 110
  • 10

2 Answers2

1

The dart:io library is not supported in DartPad. However you can use this Dart compiler, it supports dart:io.

Andrej
  • 2,743
  • 2
  • 11
  • 28
1

As noted in the dart:io documentation:

Important: Browser-based apps can't use this library. Only the following can import and use the dart:io library:

  • Servers
  • Command-line scripts
  • Flutter mobile apps
  • Flutter desktop apps
jamesdlin
  • 81,374
  • 13
  • 159
  • 204