news 2026/4/18 2:57:54

Android学Dart学习笔记第十三节 注解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Android学Dart学习笔记第十三节 注解

序言

是的没错,dart中也有注解,而且和java很像
比如这个@Deprecated、@override 都是非常熟悉的注解。
但是我们依然要过一下,目的不是深入了解dart中每个注解的实际使用场景,而是一种泛的了解。

文档描述

注解又叫Metadata

Use metadata to provide additional static information about your code. A metadata annotation begins with the character @, followed by either a reference to a compile-time constant (such as deprecated) or a call to a constant constructor.
译文:使用注解提供有关代码的额外静态信息。元数据注释以字符@开始,后面是对编译时常量的引用(例如deprecated)或对常量构造函数的调用
Metadata can be attached to most Dart program constructs by adding annotations before the construct’s declaration or directive.
译文:元数据可以通过在大多数Dart程序结构的声明或指令之前添加注释的方式附加到该结构中。

Built-in annotations 内置的注解

@Deprecated

Marks a declaration as deprecated, indicating it should be migrated away from, with a message explaining the replacement and potential removal date.
将声明标记为已弃用(deprecated),表示应该迁移该声明,并给出解释替换日期和潜在删除日期的消息。

@deprecated

和上面的很像,但是首字母不是大写
Marks a declaration as deprecated until an unspecified future release. Prefer using @Deprecated and providing a deprecation message.
将声明标记为废弃,直到未指定的未来版本。建议使用@Deprecated并提供弃用消息

和上面的主要区别就是@Deprecated可以提供弃用信息

@override

这是我们非常熟悉的,在dart中他的使用场景也是相同的
Marks an instance member as an override or implementation of a member with the same name from a parent class or interface.

将实例成员标记为父类或接口中具有相同名称的成员的覆盖或实现。

@pragma

Provides specific instructions or hints about a declaration to Dart tools, such as the compiler or analyzer.
提供有关Dart工具(如编译器或分析器)声明的特定指令或提示。

这个感觉是个新东西。暂时不深入,知道就行,

Analyzer-supported annotations (Analyzer 支持的注解详解)

Beyond providing support and analysis for the built-in annotations, the Dart analyzer provides additional support and diagnostics for a variety of annotations from package:meta.
除了为内置注释提供支持和分析外,Dart分析器还为来自meta包的各种注释提供了额外的支持和诊断。

@visibleForTesting

Marks a member of a package as only public so that the member can be accessed from the package’s tests. The analyzer hides the member from autocompletion suggestions and warns if it’s used from another package.

将包的成员标记为仅public,以便可以从包的测试中访问该成员。分析器会在自动补全建议中隐藏成员,并在其他包中使用该成员时发出警告。

通俗的讲就是虽然这个成员是public,但是有了这个注解标记之后他就不允许被其他的包中调用。
主要为了测试使用

@awaitNotRequired

Marks variables that have a Future type or functions that return a Future as not requiring the caller to await the Future. This stops the analyzer from warning callers that don’t await the Future due to the discarded_futures or unawaited_futures lints.

将具有Future类型的变量或返回Future的函数标记为不需要调用者等待Future。这阻止了分析器警告那些由于discarded_futures或unawaited_futures检查而不等待Future的调用者。

被调用者虽然是异步但不会返回结果,调用处无需await

Custom annotations

classMyAnnotation{finalString value;constMyAnnotation(this.value);}@MyAnnotation('hello')vara=0;@MyAnnotation('hello')voidmyFunction(){print('hello world');}voidmain(){myFunction();}
Specifying supported targets

指定注解支持的目标
To indicate the type of language constructs that should be annotated with your annotation, use the @Target annotation from package:meta.
要指定应该用注释标注的语言结构的类型,请使用package:meta中的@Target注释。

请看下面的例子

import'package:meta/meta_meta.dart';@Target({TargetKind.function,TargetKind.method})classMyAnnotation{finalString value;constMyAnnotation(this.value);}@MyAnnotation('hello')//The annotation 'MyAnnotation.new' can only be used on methods or top-level functions.dartinvalid_annotation_targetvara=0;@MyAnnotation('hello')voidmyFunction(){print('hello world');}

这是我们学习至今,首次用到其他包的内容我们需要添加依赖

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 18:59:56

基于springboot的水果购物管理系统的设计与实现

由于互联网技术不断进步,网络不断来到人们的身边,很多信息将会对我们的社会产生影响。生活中普遍存在的企业经营管理等方面逐渐变得有序化以及网络化。传统手工作业逐渐被现代工具所取代,网上购物系统越来越广泛。加上我国是水果种植面积和产…

作者头像 李华
网站建设 2026/4/16 15:38:53

Excel中Lookup函数实现临界点归入下一个等级的方法

Excel中经常会遇到根据得分得到相应的评级的问题&#xff0c;例如&#xff1a;0≤得分<30为智障&#xff0c;30≤得分<60为轻障&#xff0c;60≤得分<70为不合格&#xff0c;70≤得分<80为勉强合格&#xff0c;80≤得分<90为合格&#xff0c;90≤得分<100为优…

作者头像 李华
网站建设 2026/4/14 5:33:25

49、dhcpd 参考指南

dhcpd 参考指南 1. 简介 本文将详细介绍 dhcpd 命令及其配置文件 dhcpd.conf 的语法,它是 Internet Software Consortium (ISC) 动态主机配置协议 (DHCP) 服务器 ISC dhcpd 的参考资料。需要注意的是,dhcpd 仍在开发中,相关信息基于 Beta Release 5 Patch Level 16,软件后…

作者头像 李华
网站建设 2026/4/18 3:13:03

25、数据整理、可视化与关系型数据库入门

数据整理、可视化与关系型数据库入门 1. 数据整理与可视化练习 在数据整理和可视化方面,有几个有趣的练习可以帮助我们提升相关技能。 1.1 鸣禽的生活史 Martin(2015)对温带和热带环境中的鸣禽进行了研究。他发现,在面临较高巢穴捕食风险的物种中,其峰值生长率更高;而…

作者头像 李华
网站建设 2026/4/18 2:18:32

数据结构(树)

一、树的种类二、二叉排序树三、平衡二叉树四、红黑树1.每个节点不是黑色就是红色2.根节点永远都是黑色的3.叶子结点都是黑色的4.如果一个节点的颜色是红色的&#xff0c;那么他的子节点的颜色一定是黑色的5.从根节点出发到任意一个叶子结点&#xff0c;走过的路径上的黑色节点…

作者头像 李华
网站建设 2026/4/10 18:13:48

计算机毕业设计springboot餐饮娱乐行业收银管理系统 基于SpringBoot的餐饮娱乐一体化收银与运营平台 面向餐娱门店的SpringBoot智能收银与业务协同系统

计算机毕业设计springboot餐饮娱乐行业收银管理系统m6l4o933 &#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。餐饮娱乐行业在高峰时段常面临点餐、结账、库存、活动核销等多线并发…

作者头像 李华