为什么文件 class return 是类型为“_File”的对象而不是 Dart/Flutter 中的 'File'?
Why does the File class return an object of type '_File' instead of 'File' in Dart/Flutter?
没有什么可说的了。我创建了一个没有依赖项的空白项目来测试它。
如果您想自己复制 运行 代码:
import 'dart:io';
void main() {
File testFile = File('test');
print('testFile: $testFile');
print('testFile type: ${testFile.runtimeType}');
print(testFile.runtimeType == File);
print('');
}
编辑:将 flutter 升级到 2.8.0,同样的问题仍然存在。
File
是抽象的class。 _File
是 File
的实现。所以在这种情况下,testFile 的 runtimeType 是 _Type
.
@pragma("vm:entry-point")
abstract class File implements FileSystemEntity {
/// Creates a [File] object.
///
/// If [path] is a relative path, it will be interpreted relative to the
/// current working directory (see [Directory.current]), when used.
///
/// If [path] is an absolute path, it will be immune to changes to the
/// current working directory.
@pragma("vm:entry-point")
factory File(String path) {
final IOOverrides? overrides = IOOverrides.current;
if (overrides == null) {
return new _File(path);
}
return overrides.createFile(path);
}
如果您想自己复制 运行 代码:
import 'dart:io';
void main() {
File testFile = File('test');
print('testFile: $testFile');
print('testFile type: ${testFile.runtimeType}');
print(testFile.runtimeType == File);
print('');
}
编辑:将 flutter 升级到 2.8.0,同样的问题仍然存在。
File
是抽象的class。 _File
是 File
的实现。所以在这种情况下,testFile 的 runtimeType 是 _Type
.
@pragma("vm:entry-point")
abstract class File implements FileSystemEntity {
/// Creates a [File] object.
///
/// If [path] is a relative path, it will be interpreted relative to the
/// current working directory (see [Directory.current]), when used.
///
/// If [path] is an absolute path, it will be immune to changes to the
/// current working directory.
@pragma("vm:entry-point")
factory File(String path) {
final IOOverrides? overrides = IOOverrides.current;
if (overrides == null) {
return new _File(path);
}
return overrides.createFile(path);
}