Does dartpad not support I/O?

1,501

Solution 1

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

Solution 2

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
Share:
1,501
Jay
Author by

Jay

Updated on December 28, 2022

Comments

  • Jay
    Jay over 1 year

    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
    Jay about 3 years
    So, do we have to use desktop editor for that ?
  • Andrej
    Andrej about 3 years
    Yeah, that is the way to go.
  • Jay
    Jay about 3 years
    Thanks! @Andrej
  • Andrej
    Andrej about 3 years
    Hey, I just found an online Dart compiler, I left you a link in the answer.
  • Jay
    Jay about 3 years
    the compiler is otherwise great but does not give manual control over NULL safety like DartPad
  • Andrej
    Andrej about 3 years
    If you want null safety control then you should download a desktop editor. :)
  • Jay
    Jay about 3 years