0
votes

I am a student and very new to flutter/dart so I am incredibly sorry if I make mistakes along the way or just sound like a noob.

I have an application I am currently working on and I have some classes I want to import.

I want to make a simple console application, but do not copy and to always have the same updated classes how do I import the classes from a folder.

I tried the following:

made a folder called "WindowsApp"

made another folder called "lib" (because for some reason it is the only I can run the main)

made a file "main.dart"

and had the following code

import '<directory>\\foo.dart';

void main(){ 
 print('test');
}

Does it say that the directory doesn't exist? In java, I remember doing something similar and it worked, but I seem to not have the same logic work here.

The research I have done it is still foggy as to what to do because one answer is to use Source, another provided what I wanted, but didn't show the code and the code that was shown introduced a new thing called the library. Is there no simple way like java where you just put in the directory and that is it?

Do I need a full-blown project with a YAML and all if I wanted to use APIs and such?

1
Path should be like this /foo.dart , not \foo.dartRobin

1 Answers

3
votes

it seems that you have made one simple mistake, using the wrong key '\' instead of '/'

you can check for instructions on how to import packages here

and you can use the same method to import your own stuff, for example:

say you have this folder structure:

├── /lib
│   ├── main.dart
├───├── /random
│   ├───├── another.dart
---

to import another.dart to main.dart you would have this, using your example:

import 'package:<yourappname_on_pubspec.yaml>/random/another.dart';

void main(){ 
 print('test');
}

OR

import '../random/another.dart';

void main(){ 
 print('test');
}

hope this made it clear