How to install Dart on Linux and set up VIM as IDE

Dart is easy to use BSD licensed programming language to build mobile, desktop, server, and web apps. Google creates and maintains the Dart ecosystem. It follows C-style syntax. We can compile code either to the native platform or JavaScript. Flutter allows building iOS/Android mobile and desktop/web apps from a single code base. Let us see how to install Dart programming language on a Debian or Ubuntu Linux and set vim as IDE.

This page explains how to install the Dart SDK on the Ubuntu Linux desktop to build Dart command-line, server, and non-Flutter web apps.

apt command/apt-get command

First update repo:
sudo apt update
sudo apt upgrade

For older Ubuntu or Debian system, add HTTPS support for APT using the following package:
sudo apt-get install apt-transport-https
Next, install gpg keys from Google:
sudo sh -c 'wget -qO- | apt-key add -'
Set up DART SDK package repo:
sudo sh -c 'wget -qO- > /etc/apt/sources.list.d/dart_stable.list'
Finally, update the repo and then install the dart SDK for writing code:
sudo apt update
sudo apt install dart

Set up environment variable on Linux

Type the following export command:
export PATH="$PATH:/usr/lib/dart/bin"
Then update your ~/.bash_profile or ~/.profile file:

echo 'export PATH="$PATH:/usr/lib/dart/bin"' >> ~/.bash_profile

Display dart SDK version to verify that everything is working correctly:
dart --version

How to install Dart Programming Language SDK on Ubuntu or Debian LinuxHow to install Dart Programming Language SDK on Ubuntu or Debian Linux

Get the Dart SDK on Linux and verify installation

Setting up VIM as IDE for Dart

Vim-plug is a beautiful and minimalist plugin for vim. We need to use Vim-plug (or any other plugin you like) to configure VIM as IDE (Integrated Development Environment) for Linux. Of course, you can use Emacs or Intellij IDE with the Dart plugin. Edit the ~/.vimrc and add the following plugins:
Plug 'dart-lang/dart-vim-plugin'
Plug 'natebosch/vim-lsc'
Plug 'natebosch/vim-lsc-dart'

Here is how it looks:

call plug#begin('~/.vim/plugged') " Install ansible plugin 
Plug 'pearofducks/ansible-vim' " Make vim pretty with nova-vim
Plug 'sheerun/vim-polyglot'
Plug 'trevordmiller/nova-vim' " Stupid stuff
Plug 'vim/killersheep' " Ansible for IT stuff
Plug 'pearofducks/ansible-vim'
Plug 'mbbill/undotree'
Plug 'arzg/vim-colors-xcode' " Dart support
Plug 'dart-lang/dart-vim-plugin'
Plug 'natebosch/vim-lsc'
Plug 'natebosch/vim-lsc-dart'
call plug#end() " for dart
let g:lsc_auto_map = v:true

Start the vim, and then type:

Writing Hello World Dart program

Passage to any programming language is the ‘Hello World’ program. So let us start vim:
vim hello.dart
Append the code:

// My first Dart program
void main(){ print("Hello World! Let us learn Dart!!");

Run it as follows:
dart hello.dart
Installing Dart on Linux and set up VIM as IDE with Hello World

Installing Dart on Linux and set up VIM as IDE with Hello World
Dart comments usually start with //. To show text on the screen, we used the top-level print() function. Every app has a main() function. If you know the C programming language, you will learn Dart very quickly. For instance:

// Get input from the stdin and print output to stdout via dart:io 
// To access APIs defined in io libraries, use import as follows
import 'dart:io';
void main(){ print("May I know your Name please? "); /* from our io lib */ String name = stdin.readLineSync(); print("Hi $name!nLet's be friends!");

Variables in Dart

We can declare a variable without specifying its type. For example:

// Most variables don’t need explicit types
var foo = 'bar';
var message = 'You must be root to run this app';
var pi = 3.14;
// However, we can define type too
int i = 5;
double var_name = 3535335.66;
String str_name = 'some value';
// We can define PI as constant variable using const or final const PI = 3.14;
final PAUSE_VALUE = 300;

Here is if control flow along with bash for loop:

	if ( i >= 3) { print('Do something '); } else { print('Some message here'); } // for loop example to print welcome message 5 times  for (int j = 1; j <= 5; j++) { print("Welcome $j times."); }

I strongly recommend that you check out Dart’s documentation for coding samples and syntax.

A note about Vim and Dart

We added Dart language-aware tooling to vim earlier. This allows us to get function references and auto-complete function or variable names, and more:

We can go to definition by pressing:
CTRL + ]
## OR ##
CTRL + W + ]

From ~/.vimrc:

" Complete default keyboard mappings are:
let g:lsc_auto_map = { 'GoToDefinition': '<C-]>', 'GoToDefinitionSplit': ['<C-W>]', '<C-W><C-]>'], 'FindReferences': 'gr', 'NextReference': '<C-n>', 'PreviousReference': '<C-p>', 'FindImplementations': 'gI', 'FindCodeActions': 'ga', 'Rename': 'gR', 'ShowHover': v:true, 'DocumentSymbol': 'go', 'WorkspaceSymbol': 'gS', 'SignatureHelp': 'gm', 'Completion': 'completefunc', }

For all config options see vim language server client page.

Summing up

I find Dart not just easy to use but also encourages to build iOS, Andriod, Web, and desktop app from a single codebase with Flutter auspicious under Linux. Of course, to learn Flutter, you need to have fundamental concepts of Dart programming. I prefer to use Vim as IDE. But, you may want to use other IDEs that support Dart plugins. Happy coding.

Posted by Contributor