Dart 如何明确说明使用哪个包类?

srk*_*Z84 3 location geolocation dart flutter flutter-dependencies

我有一个 Dart (Flutter) 应用程序,它同时使用locationmap_view包。我的问题是这两个都定义了一个“位置”类。

如何明确说明我在任何特定调用中使用的两个类中的哪一个?

例如,我尝试在类名前加上包名:

location.Location = new Location();

map_view.Location = new Location(45.5231233, -122.6733130);
Run Code Online (Sandbox Code Playgroud)

但是 Dart 似乎并不喜欢这种语法。

srk*_*Z84 7

找到了答案。您必须按照以下方式为图书馆提供前缀:https : //www.dartlang.org/guides/language/language-tour#libraries-and-visibility

之后,您可以使用类名中的前缀使其工作:

import 'package:location/location.dart' as locLib;
import 'package:map_view/map_view.dart' as mapViewLib;

locLib.Location = new Location();
mapViewLib.Location = new Location(45.5231233, -122.6733130);
Run Code Online (Sandbox Code Playgroud)