Solved: Why does the File class return an object of type '_File' instead of 'File' in Dart/Flutter?


Asked by Nathan T on December 16, 2021 (source).

enter image description here

enter image description here There isn't much else to be said. I created a blank project with no dependencies to test this.

Code to copy if you want to run it yourself:

import 'dart:io';

void main() {
  File testFile = File('test');
  print('testFile: $testFile');
  print('testFile type: ${testFile.runtimeType}');
  print(testFile.runtimeType == File);

Edit: upgraded flutter to 2.8.0, same problem persists.


Question answered by dangngocduc (source).

File is abstract class. _File is implement of File. so on this case runtimeType of testFile is _Type.

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.
  factory File(String path) {
    final IOOverrides? overrides = IOOverrides.current;
    if (overrides == null) {
      return new _File(path);
    return overrides.createFile(path);