Split dart files

1,486 views
Skip to first unread message

Mitch Besser

unread,
Dec 11, 2018, 6:43:20 AM12/11/18
to Flutter Dev
I'm used to C++ and having a linker.  I'm looking for how to make Android Studio understand multiple .dart files, compile and link them. 

Can anyone direct me to materials that explain projects with multiple .dart files?

Thanks,

Mitch

Steven McDowall

unread,
Dec 11, 2018, 7:54:34 AM12/11/18
to Mitch Besser, Flutter Dev
I think the Flutter plugin takes care of all the heavy lifting ..

I’ve never had to deal with that .. just organize your folders under lib for your source code and import them as needed .. 

Maybe going through a tutorial or 2 to grok Dart and Flutter or a course from Udemy or where ever? 

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Greg Spencer

unread,
Dec 11, 2018, 11:01:49 AM12/11/18
to Steven McDowall, bess...@gmail.com, Flutter Dev
Hi Steven,

Try checking out these resources:

Dart uses "libraries" and published "packages" to collect .dart files, and a lot of how that happens comes from the directory structure (which files are next to each other in a directory), the Dart compiler, and the pubspec.yaml file.  Also take a look at some Dart projects and see how others have organized their code.

-Greg.

Steven McDowall

unread,
Dec 11, 2018, 11:23:45 AM12/11/18
to Greg Spencer, bess...@gmail.com, Flutter Dev

Thanks Greg -- but I wasn't the one asking -- that was Mitch .. But its good resources

Greg Spencer

unread,
Dec 11, 2018, 4:16:18 PM12/11/18
to Steven McDowall, Mitch Besser, Flutter Dev
Oops, sorry, I just misread.

Mitch Besser

unread,
Dec 11, 2018, 4:40:47 PM12/11/18
to Flutter Dev
Thanks for the suggestions. I already read those links and have done a few courses online. I've searched for things specific to multiple files, but all of the things I've found so far either are a single file, or a library. Android Studio does not appear to be doing the linking. if I just split working code into two files, it fails.

I think there is something fundamental I'm misunderstanding. Maybe be there is no concept of an object file to link in Dart? Maybe some other mechanism? I see there is a way to assemble multiple files with a #include like facility to have "part" files, but that would not help with circular file links and is difficult except in very simple circumstances since I'd have explicitly list every dependency in the source.

Anyone know of the mechanism expected to link so I can find materials to read?

If this was C or C++, the build model would be source code to object code to linker to executable. Flutter/Dart must have some build model? Is it all one file named main.dart? Is it all done exclusively with library files?

Help? Thanks.

Michael Thomsen

unread,
Dec 11, 2018, 4:54:39 PM12/11/18
to bess...@gmail.com, Flutter Dev
Have you added imports from your main.dart to the other .dart files?

As a concrete example, look at how this Shrine demo app is configured:


Greg Spencer

unread,
Dec 11, 2018, 6:11:23 PM12/11/18
to Michael Thomsen, Mitch Besser, Flutter Dev
The Dart compiler has many modes, but one of them is to run in a Dart Virtual Machine (VM), so the build model for Dart is closer to that of Java than C or C++.

There are no .o files, or .a files, or .lib's, .dlls, or .so's that you have to deal with, only libraries and packages, and those are composed only of source code that is parsed, compiled and "Just In Time" optimized on demand at runtime when Dart is running in a VM.

When Dart is run in an "Ahead of Time" (AOT) compilation mode, it gets parsed, compiled and optimized at compile time. In AOT mode, the compiler can generate a "snapshot", which is akin to a dynamic library or executable that the runtime loads when it runs, which is about the only build product that gets physically manifested on disk (I'm simplifying this a bit: there are .dill (yes, with an "i") intermediate files, but you don't generally need to create or care about those).

When being converted to JavaScript to run in a browser, it gets parsed, optimized, and compiled into JavaScript, and the JavaScript JIT VM takes care of more optimization.

I know is seems odd to go straight from source to executable, but in regular usage you basically don't need to care about any intermediate forms for the language: the Dart runtime/compiler should handle it for you. How you build/run Dart code depends a lot on your use case for it. For Flutter apps, you shouldn't need to care more than telling the flutter tool where to find the entry point .dart file (main.dart), and where to put the built application.

It sounds like your main roadblock of splitting up files has more to do with structuring of your source code directories and importing other files into your main.dart, as Michael pointed out.

You probably just need to add something like

import 'my_other_file.dart';

in your main.dart (assuming the files are next to each other in the same directory).

I'd suggest looking at some more concrete examples, like maybe starting with using flutter create to make a "hello world" that you can look at.


-Greg.

Mitch Besser

unread,
Dec 12, 2018, 3:07:33 AM12/12/18
to Flutter Dev
Wow.. thanks!  I wasn't expecting a full answer, just a link or basic description to send me in the right direction. It sounds like import is the mechanism expected and I must do that in order to allow calling across multiple dart files. I was not expecting that to be the mechanism. At first I just assumed it would just work. Seems annoying, but if that's the way it works, I'm not going to argue. When it didn't, I thought, okay, maybe I need to add something to a build script or linker statement or.... 

I verified it worked with a circular import dependency, so I'm okay with import. (That's not saying I like it, but I can live with it). 

I'll check out the links too. 

Again, thank you for the excellent answer. 
Reply all
Reply to author
Forward
0 new messages