KUMAR KESHAV
KUMAR KESHAV

Reputation: 23

How to access a method from one dart file to another?

I have created this method in another dart file and i have to access it in main dart file. I don't know how to access this as it only belongs to that class. I am a beginner in flutter and firebase(as shown below is firebase code).

 DocumentReference db = Firestore.instance.collection('todolist').document(taskName);
 db.delete().whenComplete(() {
   print ('object deleted');
 });
}  ````

Upvotes: 1

Views: 159

Answers (1)

Paul Groß
Paul Groß

Reputation: 247

You could make a library out of your first file:

library firestore_utils;

void deleteObject(){
 DocumentReference db = Firestore.instance.collection('todolist').document(taskName);
 db.delete().whenComplete(() {
   print ('object deleted');
 });
}

Now you should be able to import the lib inside an other file and use the function deleteObject(). Note that functions are invisible outside a lib or class if their name starts with "_", f. e "_deleteObject()"

Upvotes: 1

Related Questions