63

What does external mean in Dart? For example: external DateTime._now();

I'm new to Dart, I can't find documentation for external, so can you give an example to help explain?

Austin Cummings
  • 770
  • 1
  • 8
  • 15
幕阜山道友
  • 753
  • 1
  • 6
  • 6
  • 6
    _"i can't find doucment for `external`"_ http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-408.pdf section 9.4 – Michael Jul 24 '14 at 09:19

6 Answers6

62

9.4 External Functions
An external function is a function whose body is provided separately from its
declaration. An external function may be a top-level function (17), a method

The body of the function is defined somewhere else.
As far as I know this is used to fix different implementations for Dart VM in the browser and Dart VM on the Server.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
25

When we make an external function inside a class like toString()

external String toString();

means this method is abstract and the child of the parent class will add the function body, that's because in Dart we only can make an abstract class.

Summary:

external function = abstract function in not abstract classes

iDecode
  • 22,623
  • 19
  • 99
  • 186
Mina Samir
  • 1,527
  • 14
  • 15
9

I don't think external keyword is meant to be used to mark methods as abstract, even if that's possible

It's enough to leave a method with no implementation to set it abstract, inside an abstract class

It's the equivalent of declare in TypeScript, and extern in C#, those are used for interoperability with other runtimes, which means you're telling the compiler "Don't worry about this method's implementation, I promise it will exist at runtime", the runtime may be in C or Javascript or whatever

Mohamed Ali
  • 3,717
  • 1
  • 33
  • 39
5

In case, if you are wondering why or where should I even use external keyword, here is a one more example for Flutter.

class MyStruct extends Struct {
  @Int32()
  external int a;

  @Float()
  external double b;

  external Pointer<Void> c;
}

Sometimes, but not often when you play with native libraries, in this case with Struct to access native struct's field in memory. Under Struct We must declare all fields as external because it will be external fields from dart:ffi (C / C++).

So, external is more than just way to declare "abstract method".

just-Luka
  • 377
  • 1
  • 6
  • 19
4

9.4 External Functions An external function is a function whose body is provided separately from its declaration.

What this does mean is that you define the function, but without implementation. It's exactly how you define the abstract method, but the only difference is that with external you don't implement the method in dart but in C or something else.

Something like String class it can be considered as external functions except that the String class it marked with @pragma('vm:entry-point') which make the entire class use native code.

See the following example to understand:

This dart's side.

https://github.com/dart-lang/sdk/blob/main/sdk/lib/core/string.dart#L711

This the implementation in C++.

https://github.com/dart-lang/sdk/blob/main/runtime/lib/string.cc#L473-#L478

Talat El Beick
  • 423
  • 6
  • 12
  • It would be helpful if you gave a source for: 9.4 External Functions An external function is a function whose body is provided separately from its declaration. – Worik Jul 12 '23 at 23:36
2

In my opinion it is an equivalent of Java native keyword. For example, since current time milliseconds is implemented differently on Android, iOS, Linux etc, DateTime.now().millisecondsSinceEpoch will be linked to different implementations at runtime. So it is not initially known how this method will look like. For this reason it is marked as external meaning it is platform dependent.

Kairat
  • 698
  • 1
  • 11
  • 14