Ashtav
Ashtav

Reputation: 2966

How to call functions in other class without type class at first time?

for example, I have a class named Fn, in Fn I have function named myFunc, so I use and call the function is like this

import 'fn.dart';

Fn().myFunc();

I want just type

myFunc();

How I can do that? thank you so much for your help.

Upvotes: 0

Views: 593

Answers (1)

Doc
Doc

Reputation: 11651

Every time you say Fn().myFunc(); you are creating an instance of Fn which creates useless objects every time.

What you should do is

Fn fn = Fn(); and then fn.myFunc(); everytime you want to call the function

Further, if you want then you can create static methods like

class A {
  static void bar() {} // A static method
  void baz() {} // An instance method
}

then we have

A.bar(); for static method

or

A a = A(); and a.baz(); everytime you need to call baz, which is an instance method.

Note: you can have top-level function outside of class also in dart. Read https://www.dartlang.org/guides/language/language-tour

Upvotes: 3

Related Questions