- Dart:Scalable Application Development
- Davy Mitchell Sergey Akopkokhyants Ivo Balbaert
- 555字
- 2021-07-09 18:56:20
Adding metadata
At the time of writing, some of the fullscreen APIs are marked as experimental in the Dart API documentation, and this is made clear in the documentation as well. By the time you read this, the fullscreen APIs should hopefully be stabilized and fully operational.
This is the normal process for new web browser features, which are eventually made stable. The adding of experimental APIs is likely to be a continuous process—it seems very unlikely that web browsers will ever be 100 percent complete! The onFullscreenChange
Dart API documentation reads:
Experimental ElementStream<Event> get onFullscreenChange Stream of fullscreenchange events handled by this Element.
The source code for this part of the dart:html
package has several annotations including the @experimental
marker:
/// Stream of `fullscreenchange` events handled by this [Element]. @DomName('Element.onwebkitfullscreenchange') @DocsEditable() // https://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html @Experimental() ElementStream<Event> get onFullscreenChange => fullscreenChangeEvent.forElement(this);
Dart has a very flexible annotation system, and new systems can be created to add metadata to most of the components in Dart, such as classes, functions, and libraries. The pattern, as you probably already spotted, is used to start them with a @
sign followed by a call to the const
constructor or a reference to a compile-time constant variable.
Creating a custom annotation
Let's create our own Temporary
annotation that will mark classes as temporary to the design of the application and will allow a label to record which planned build the temporary classes are scheduled to be removed:
import 'dart:mirrors'; class Temporary { final String removalBuildLabel; const Temporary(this.removalBuildLabel); String toString() => removalBuildLabel; } @Temporary('Build1') class TempWidgetTxt {} @Temporary('Build2') class TempWidgetWithGFX {} void main() { ClassMirror classMirror1 = reflectClass(TempWidgetTxt); ClassMirror classMirror2 = reflectClass(TempWidgetWithGFX); List<InstanceMirror> metadata1 = classMirror1.metadata; print(metadata1.first.reflectee); List<InstanceMirror> metadata2 = classMirror2.metadata; print(metadata2.first.reflectee); }
The Temporary
constructor allows a label to be set. This field must be final
(a single-assignment variable or field) if this class is to be used as an annotation. The main function calls reflectClass
to obtain the details of the classes and then accesses this metadata and stores it in a list. The toString
method allows the metadata to be the output via print
:
Build1 Build2
Annotations are used extensively in the core Dart APIs, and in packages, they are used as the basis of frameworks, for example, web applications. They are not an obscure feature of the core SDK.
Translating the user interface text
My first ever professional software release did not involve any creation of code. It was a language update release for an existing package, and it was just as satisfying to mail out the CD-ROM (this was a while ago!) as something I had coded from scratch. Thankfully, it made me famous in Norway!
The translation of a software application is critical for its acceptance in some markets. This is even more true in the world of web applications. Dart has a package called intl
that is used to help bring your applications to a global audience.
The presenter
interface needs to be updated so that it displays the editor in French and Spanish. This will involve extracting the strings that we wish to use, obtaining translations, and integrating them back into the project.
The language will be selectable at runtime by user selection. The current OS interface language may not be the language that the user wishes to use the application in.
- C# 2012程序設計實踐教程 (清華電腦學堂)
- Building a Game with Unity and Blender
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- Machine Learning with R Cookbook(Second Edition)
- 跟老齊學Python:輕松入門
- C#程序設計教程
- Unity 5.x By Example
- HTML5入門經典
- Nginx實戰:基于Lua語言的配置、開發與架構詳解
- HTML5秘籍(第2版)
- OpenResty完全開發指南:構建百萬級別并發的Web應用
- Julia for Data Science
- Python Data Science Cookbook
- SciPy Recipes
- 寫給程序員的Python教程