いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

Flutterメモ-30 (dart の REPL)(interactive)

概要

以下の記事で知りました。これは便利そうですね。

flutterawesome.com

試してみる

丁度良い機会なので、WSL2環境にdartをインストールするところからやってみます。

Homebrew経由でdartをインストールしたいので、dart-lang/dart を tap

$ brew tap dart-lang/dart

それから、dart をインストール

$ brew install dart

$ dart --version
Dart SDK version: 2.18.3 (stable) (Mon Oct 17 13:23:20 2022 +0000) on "linux_x64"

$ dart --disable-analytics
  ╔════════════════════════════════════════════════════════════════════════════╗
  ║ Analytics reporting disabled. In order to enable it, run:                  ║
  ║                                                                            ║
  ║   dart --enable-analytics                                                  ║
  ║                                                                            ║
  ╚════════════════════════════════════════════════════════════════════════════╝

2022-11-02時点での最新版がインストールされました。

次に、REPLである interactive をインストール

$ dart pub global activate interactive
+ _fe_analyzer_shared 50.0.0.6s)
+ analyzer 5.2.0
+ args 2.3.1
+ async 2.10.0
+ cli_repl 0.2.3
+ collection 1.17.0
+ convert 3.1.1
+ crypto 3.0.2
+ file 6.1.4
+ glob 2.1.0
+ interactive 1.2.0
+ js 0.6.5
+ logging 1.1.0
+ meta 1.8.0
+ package_config 2.1.0
+ path 1.8.2
+ pub_semver 2.1.2
+ source_span 1.9.1
+ string_scanner 1.2.0
+ term_glyph 1.2.1
+ typed_data 1.3.1
+ vm_service 9.4.0
+ watcher 1.0.2
+ yaml 3.1.1
Downloading interactive 1.2.0...
Downloading path 1.8.2...
Downloading logging 1.1.0...
Downloading collection 1.17.0...
Downloading cli_repl 0.2.3...
Downloading args 2.3.1...
Downloading analyzer 5.2.0...
Downloading _fe_analyzer_shared 50.0.0...
Downloading meta 1.8.0...
Downloading js 0.6.5...
Downloading yaml 3.1.1...
Downloading watcher 1.0.2...
Downloading package_config 2.1.0...
Downloading crypto 3.0.2...
Downloading typed_data 1.3.1...
Downloading string_scanner 1.2.0...
Downloading vm_service 9.4.0...
Downloading pub_semver 2.1.2...
Downloading glob 2.1.0...
Downloading source_span 1.9.1...
Downloading term_glyph 1.2.1...
Downloading convert 3.1.1...
Downloading file 6.1.4...
Downloading async 2.10.0...
Building package executables... (5.2s)
Built interactive:interactive.
Installed executable interactive.
Warning: Pub installs executables into $HOME/.pub-cache/bin, which is not on your path.
You can fix that by adding this to your shell's config file (.bashrc, .bash_profile, etc.):

  export PATH="$PATH":"$HOME/.pub-cache/bin"

Activated interactive 1.2.0.

パスの設定は後でするとして、一旦環境変数PATHを変更しておく

$ export PATH="$PATH":"$HOME/.pub-cache/bin"

$ interactive
Run: dart [--enable-vm-service=40491, file:///home/dev/.pub-cache/global_packages/interactive/bin/interactive.dart-2.18.3.snapshot, --vm-service-was-enabled]
The Dart VM service is listening on http://127.0.0.1:40491/EdBi_Op2d2o=/
The Dart DevTools debugger and profiler is available at: http://127.0.0.1:40491/EdBi_Op2d2o=/devtools/#/?uri=ws%3A%2F%2F127.0.0.1%3A40491%2FEdBi_Op2d2o%3D%2Fws
Workspace: /tmp/dart_interactive_workspace_2022-11-02T145529907367
>>>

起動しましたね。

少し遊んでみます。

>>> class A {
...     late int val;
... }
>>> a = A()
Instance of 'A'
>>> a.val = 999;
>>> print(a.val);
999

>>> !pwd
/tmp/dart_interactive_workspace_2022-11-02T145529907367

>>> !ls
lib
pubspec.yaml

>>> !dart pub add crypto
Resolving dependencies...

+ collection 1.17.0
+ crypto 3.0.2
+ typed_data 1.3.1

Changed 3 dependencies!

>>> import 'package:crypto/crypto.dart';
>>> import 'dart:convert';
>>> b = utf8.encode('helloworld');
>>> d = sha1.convert(b);
>>> d
6adfb183a4a2c94a2f92dab5ade762a47889a5a1

サクサク動いていい感じですね。

参考情報

www.indetail.co.jp

dev.to

flutter.dev


過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。