📜 ⬆️ ⬇️

Dart: how to start acquaintance with the language?

image


Hello! Last week we held the second annual meeting of the Russian-speaking community Dart (aka Dartup) and received very informative and useful feedback from people who came to the event. This year we were about 130 people. If you were not and want to see, here is the link to the entries. Thanks for the mitap to all participants, organizers and partners.


Many guests on the sidelines and in the telegram channel dedicated to the meeting asked us why, with great expertise in the language, we still did not think about the Dart Academy, webinars, or at least a brief guide for beginners. I confess that in order to start such a process, we needed an impulse from you. We got it. Therefore, we are starting a series of articles for those who are interested in the language and want to try to figure it out. And if you like and use this information, then we, as a company, will be able to devote more time to it.


About what

Let's start with a basic plan:


  • Where to find out more about Dart?
  • How and where to try?
  • Setting up the development environment
  • Running Hello world

Where to find out more about Dart?


Oddly enough https://www.dartlang.org . There is also a mini- course on syntax and language features. And since Dart is a whole ecosystem, then, as in Java, it is necessary to study standard libraries, for this there is a library tour . And if after reading you are seriously interested in the possibilities of the language, be sure to pay attention to the Effective Dart guide - the tips outlined in it will help make your code highly effective. For those who love the dry API language instead of text, here is a great link .


Dart, like JavaScript, is standardized by ECMA-408 , in this document all the nuances of the language are described in sufficient detail.


Spheres of application


Dart is a universal language. With it, you can create command-line utilities, server applications, engage in web development, and even make applications for mobile platforms. Below we will tell about it in more detail.


CLI


CLI, the whole tart of darta is written in the language itself. That is, pub , analyzer , dartdoc , dartfmt, dart2js - everything is created using Dart itself, you can see the source on the links. Utilities simply write with the fact that there is a manual .


We have an application written on Darta running on the backend in production, it uses the database and is actively working with sockets. We also use our cli-utilities to facilitate assembly or code generation. In more detail about development on DartVM it is possible to read here .


One very cool feature of DartVM is the launch of the debugger and profiler, which is included in DartVM itself and is called Observatory . According to Google, the debugger has almost no effect on the performance of the executable code, while using this tool you cannot replace the executable source code, but you can change the state.


Web


Everything is quite simple here - you can use different bindings through js-interop to the libraries of the js-world. Yes, you need to write vraperry, but Dart and JS are different worlds, so you have to pay for typing and code support.


You can write on pure Darth without frameworks or use the AngularDart spacecraft. In Wrike we use AngularDart to sell for the main product (at the moment an application is written in pure Darth, in a minified form occupying the client’s 15Mb), so we can say that it is time-tested and ready for combat conditions. And don't be confused by versioning, the paths between TS and Dart version diverged from version 2.0 (again: the Angular versions for TypeScript and Dart do not match!), Now they are different worlds with similar APIs.


There is also native redux and over_react .


Android / iOS


Dart offers a unified approach to development on both mobile platforms at once - Flutter . While it is alpha, but it works very fast and stable. In the company, we are just starting to make prototypes on it, so for now we cannot boast with great expertise. But, according to preliminary estimates, flutter can be useful to us in order to unify the business logic and simplify development.


The minimum size of the application for Android in the release of the mod weighs about 5MB + everything you write yourself. Together with the flutter, Google promotes tools for mobile development, which makes it very simple. I especially want to mention the feature of Hot Reload , which makes development for mobile devices fantastic.


How and where to try the language?


Online




If you do not want to install anything and just want to "feel" the language, you need to go to DartPad , where you can rummage around the code or download examples . There is a restriction on the connection of external packages, but the standard library at first is enough for everyone. In this case, you get a full-fledged editor with backlighting, an assistant and compiler error output.


Offline


You can use various editors for this :



→ Detailed instructions .


Each of them will use the dartanalyzer server, so you will get the same functionality support in almost all editors.


Setting up the development environment


You need to put any of your favorite editor that supports Dart.
Next, create a project:



Either path uses the stagehand package, here everyone chooses their own version. For example, I will use the 'Bare-bones Web App' template, which does not use any framework, everything is written in pure Dart using the standard ' dart: html ' library for manipulating the house-tree.




After creating the project template, you must download all the dependent packages. This is done with one simple command:
pub upgrade 

After executing the command, we will create two files.
.packages is a file with links to packages.
.pubscpec.lock is a file with specific versions of packages that pub has split.


In the world of Dart pub serves as a package manager, all available packages you can see here . Also pub knows how to publish packages, plus there are two more possibilities that will soon leave pub - this is building the project and launching the http server to start the project in the browser if we write under the web.


Now run our project:


 pub serve 

Console:




After all the “gestures” in the pub are completed, the address will appear on the screen through which you can open our example in the browser. Here you will encounter the problem that when you open localhost: 8080 pub will launch something inside of itself again - this is dart2js. It will take a little more time - depending on the size of the executable code. And here we come to the main reason why Google removes serve functions from pub, promoting faster solutions based on build_runner and Bazel , but about them in the next article.
')
In the meantime, we can run our project in DDC mode, which makes the build “incremental” and fast.
 pub serve --web-compiler=dartdevc 

Console:




In order not to write the choice of the compiler in the console every time, just uncomment it or add the lines to the very end of the pubspec.yaml file:


 web: compiler: debug: dartdevc 

Then, when calling the pub serve command, the necessary compiler will be automatically selected.


If you need to build a bundle for production, it’s enough to call


 pub build 

and you get everything you need to run the application in the build folder.


While on this stop. Ask questions in the comments and write suggestions about topics for future articles in this series. I will be glad to answer.


In the following releases
  • Let's talk about the Analyzer and how to prepare it.
  • We’ll touch on the problem of lint rules so that the code is beautiful
  • Project structure or why the lib folder is needed
  • Build: your rules
  • Tests and coverage
  • Various preprocessors css (less, scss)
  • Sprites SVG, PNG
  • Internalization / Localization of the project using intl
  • Angular in the project
  • Code Generation
  • Immunity data
  • Serialization deserialization
  • Backend
  • Isolates or how to parallelize using the isolate package
  • Websocket and http request, data transfer between the Client <-> Server

Other materials of the Russian-speaking community Dart

Source: https://habr.com/ru/post/343988/


All Articles