简体   繁体   中英

Does dartpad not support I/O?

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??

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM