goldilocks
goldilocks

Reputation: 161

How do you import files in flutter?

I'm facing an issue where I'm trying to split the code up into different files (to make it neater). So far the import is working except for one file.

Currently, my flutter project is setup in this manner enter image description here

However, one of my files is not importing correctly. This is my chorusSearch.dart file: enter image description here enter image description here

enter image description here

I've tried importing a class from chorusPage.dart. But the import is not resolving. What is wrong here? This is my chorusPage.dart file.

enter image description here

Upvotes: 1

Views: 1426

Answers (1)

Patrick G.
Patrick G.

Reputation: 475

You cannot call a method or a function of a class that should be instantiated. You are trying to access a method inside a private class (with the _ char before) of a class that should be initialized before.

If you want to create functions that don't require a view, a page and nothing visual you should make public and static classes and create something like an utility.dart class that contains them.

Then you can call Utility.yourfunction() from where you want

A little example

file utility.dart in lib/utils/

void your_method() {
//do something
}

then in place you can import the file utility.dart and you can also assign to it a name with "as" like this -> import 'package:appname/utils/utility.dart' as Utility;

then you can use Utility.your_method()

Upvotes: 1

Related Questions