无类镖文件的用途是什么?

我想知道为什么会有.dart个文件没有类。有什么好处吗?

例如:

import 'package:http/http.dart' as http;

您必须使用“ as”引用它,然后您可以像这样使用它:

 response = await http
          .post(
            url,headers: header,body: dataToSend,)

请以我为基本的开发人员,可能的答案就在网站上。

peak288 回答:无类镖文件的用途是什么?

Dart库可以包含类和其他顶级声明,这与Java不同,例如Java中,类是唯一允许的顶级声明,而所有其他声明都在类内部。

关于仅导出非类声明的库,没有什么神奇的。这只是一个图书馆。 您没有必须导入带有前缀(例如as http)的库,也可以直接导入它,然后可以引用不带前缀的导入名称:

import "package:http/http.dart";
...
   response = await post(url,headers: headers,body: dataToSend);

同样,带有类的库也可以使用前缀导入:

import "dart:convert" as convert;
... 
   Uint8List utf8 = ...;
   String result = convert.utf8.decode(utf8);

这样可以避免名称冲突,例如此处的utf8名称。 实际上,package:http/http.dart确实导出了classes,就像ResponseClient一样。

唯一的区别是,要导入 前缀的库可以选择为其顶级成员使用更短的名称,而不会冒冲突或歧义的风险。因为该名称将始终在前缀旁边使用,所以从库导出的名称中不必包含前缀的额外上下文。

上面的post可能与名为post的其他事物冲突,但是http.post明确表明了我们在谈论哪种post

,

Dart允许您指定导入前缀as。使用function print(width,height){ var cDiv = document.createElement('div'); cDiv.setAttribute('id','mainContainer'); cDiv.innerHTML='<div id="mapContainer"></div>'; var jqMapContainer = $("#map"); mapContainer = jqMapContainer[0]; var origDisplay = [],origMapParent = mapContainer.parentNode; body = window.document.body; childNodes = body.childNodes; // hide all body content $.each(childNodes,function (i,node) { if (node.nodeType === 1) { origDisplay[i] = node.style.display; node.style.display = 'none'; } }); body.appendChild(cDiv); var rc = document.getElementById('mapContainer'); rc.appendChild(mapContainer); $("#mainContainer").width(width); $("#mainContainer").height(height); $(mapContainer).width(width); $(mapContainer).height(height); setTimeout(function () { window.print(); },4000); var _self = this; // allow the browser to prepare before reverting setTimeout(function () { // put the chart back in origMapParent.appendChild(mapContainer); $("#mainContainer").remove(); // restore all body content $.each(childNodes,node) { if (node.nodeType === 1) { node.style.display = origDisplay[i]; } }); google.maps.event.trigger(map,'resize'); },4000); } ,您可以为导入的库或文件指定一个名称,然后使用该名称访问属性。通常这样做是为了防止库污染您的名称空间或使代码可读。

并且我们使用不带类的dart文件来声明将在我们的代码中重复的常量或函数,因此我们可以随时访问它而无需一遍又一遍地定义

本文链接:https://www.f2er.com/3104320.html

大家都在问