29

Is there a REPL for Dart to experiment with?

I tried inputing dart code in DevTools in Dartium and that also didn't work.

So I couldn't find an easy way to play with various APIs in dart.

ch271828n
  • 15,854
  • 5
  • 53
  • 88
ducky
  • 1,113
  • 1
  • 11
  • 23
  • **Since** 2022.10.22, there is a REPL for Dart: https://github.com/fzyzcjy/dart_interactive (disclaimer: I wrote that package) – ch271828n Oct 23 '22 at 03:58

5 Answers5

14

I tried inputing dart code in devtools in Dartium and that also didn't work.

I'm very new to Dart, but something I came across was that you CAN evaluate code in Dartium. In order to do so, you must first load a page with Dart code in it and then toggle this selector in the console from "javascript page context" to one that references a Dart package or Dart code.

enter image description here

Once you do that you should be able to execute Dart in the console:

enter image description here

As a VIM user, I hardly have to open the Dart Editor now :). I should also mention that breakpoints, hovering over stepped into code to get variable details, navigating the call stack, and some level of intellisense in the console also work. I couldn't get conditional breakpoints to work, though.

jriggins
  • 668
  • 9
  • 15
6

Though it is not really a REPL, you may find the Try Dart online tool useful for playing around. It's a bit slow, since it is actually compiling the Dart code to JavaScript in order to have it work within the browser.

There is also a console that someone built, which is probably better if you're looking for a real REPL, but it requires a bit of setup.

Pixel Elephant
  • 20,649
  • 9
  • 66
  • 83
  • I tried to build dart-console but I don't think its compatible with the new dart-sdk [0.6.15.3_r25822]. – ducky Aug 08 '13 at 11:50
  • I noticed that there is an [issue](https://code.google.com/p/dart/issues/detail?id=4409&thanks=4409&ts=1344375769) on google code for it. – ducky Aug 08 '13 at 18:06
5

There is an announcement about REPL for Dartium - see Nathanial's comment below. There are plans for Smalltalk like super-REPL. Here is what Gilad Bracha (member of the Dart team at Google) wrote on this subject in Is there a REPL or console for Dart:

"I don't see this as a language question at all. It is a matter of tooling and reflective library support. With proper mirror builder APIs, building a REPL would be trivial. As it sands right now, it can be quite challenging. And of course, REPL is not the ultimate goal - there are more advanced interactive tools, like workspaces in Smalltalk/Self/Newspeak, where you not only evaluate things interactively at some top level, but can inspect the resulting objects, evaluate within the scope of an individual declaration or object etc. I am sure we will get there in time - and i much prefer sooner than later."

Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • According to news.dartlang.org, Dartium got a REPL on the 2nd of this month. That being said, I can't seem to find it. Just typing dart into the console doesn't seem to work for me, even at break points. http://news.dartlang.org/2013/10/faster-dart2js-and-new-html-editor-in.html – Nathanial Oct 24 '13 at 01:26
  • @Nathanial You are right, it mentions REPL. I better edit my answer. – Paul Jurczak Oct 24 '13 at 05:26
  • Ok so this a long time between comments, but thank you @Nathanial for the pointer to that article. The REPL mentioned there is just expression eval on breakpoints which has no been available for a long time in the Dart debugger, but the *magic* tidbit in that article is that you can use JS style IIFE to execute which is super useful! – Maks Apr 25 '22 at 10:03
1

The correct answer is https://dartpad.dev/

That site didn't exist when the other answers were posted in 2013, but you've stumbled on this post after 2020. And now you know. https://dartpad.dev allows you to create and share new Dart snippets and even put them in a Flutter app running online. Very, very cool!

enter image description here

Rap
  • 6,851
  • 3
  • 50
  • 88
  • 2
    Nice one, however a REPL it is not :-( – Eric Platon Apr 05 '21 at 00:24
  • What? What am I missing? Read-Evaluate-Print-Loop... check, check, check, and check. Educate me, please, @EricPlaton. :-) – Rap Apr 06 '21 at 02:40
  • @Rap Dartpad is a very nice tool, but it is *not* a REPL. It does *not* maintain state as a REPL is expected too, nor does it read input nor evaluate/print Dart code in a loop. It is simply a text editor+Dart compiler and then runs the compiled code, which is completely different to a REPL. – Maks Apr 25 '22 at 09:38
1

Since 2022.10.22, there is a REPL for Dart: https://github.com/fzyzcjy/dart_interactive :)

Features:

  • Use any third-party package freely
  • Auto hot-reload code anywhere, with state preserved
  • Supports full grammar in REPL
  • Play with existing code side-by-side

Disclaimer: I wrote that package.

ch271828n
  • 15,854
  • 5
  • 53
  • 88