dartangular-dartdart-polymerdart-htmldart-webui

How to get user input on dartpad online?


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

Solution

  • 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.