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

Issue

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);
  print('');
}

Edit: upgraded flutter to 2.8.0, same problem persists.

Solution

File is abstract class. _File is implement of File. so on this case runtimeType of testFile is _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);
  }

Answered By – dangngocduc

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *