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.

Installing Dart on Ubuntu Linux

Please note that Flutter version 1.21 includes the full Dart SDK. In other words, skip the following if you have Flutter installed on your Ubuntu or Debian Linux desktop.

Install required packages using 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- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
Set up DART SDK package repo:
sudo sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /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

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:
:PlugInstall

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

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.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz