How to get user input on dartpad online?

12k views Asked by At

How can I get user input in console on dartpad?

Whenever i write

import 'dart:io';
void main() {
  stdout.write("What's your name? ");
  var name = stdin.readLineSync();
  print("Hi, $name!");
}

But I got an exception in console window like this

Uncaught exception:
Unsupported operation: StdIOUtils._getStdioOutputStream
3

There are 3 answers

0
julemand101 On BEST ANSWER

As described on the manual for DartPad: https://dart.dev/tools/dartpad

DartPad supports dart:* libraries that work with web apps; it doesn’t support dart:io or libraries from packages. If you want to use dart:io, use the Dart SDK instead. If you want to use a package, get the SDK for a platform that the package supports.

So you cannot use stdin/stdout since it comes from the dart:io library.

For a solution to your problem, you can enable "Show web content" at the bottom of the DartPad page. Here you can add HTML code to your project and make a text field with e.g. a button. Add some logic in your Dart code which listen on the button and read the value of the text field.

0
Mostafa Wael On

Unfortunately, you can't use libraries from packages like dart:io, as DartPad does only supports libraries that work with web apps.

To use those libraries:

  1. you can download the SDK

OR

  1. check the following link to run your code on VScode: link
0
Shimon Doodkin On

I have found a solution

import 'dart:js';

// While in DartPad website.
// to overcome browser security.
// Open JavaScript inspector (right click somewhere and click inspect)
// and run once the code below, (paste in console tab and press enter)
// then close the JavaScript inspector.
//
//     frame.Sandbox += ' allow-modals';
// 
String? prompt(String? text, [String? value=""]){
  return context.callMethod('prompt', [text, value]);
}

example:

void main() {
  var line=prompt("enter your name");
  print("name: $line");
}