Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import all Dart files from a Directory?

I am new to dart so please bear with me if this is really bad question. I am developing an app using flutter, and I have one question. I have many screens in my app, like About the App, Homepage, Upcoming Events, etc. I have kept all these screens in a folder in lib/screens directory. And to import them in main.dart for routes I have to import each and every file, like

import "screens/homepage.dart";
import "screens/aboutTheApp.dart";
import "screens/upcomingEvents.dart";

Is there a simpler way to do it? Is there a way to import the "screens" directory at once?

I have tried to import the complete folder and tried making it a package, but it isn't helping.

like image 776
Rahul Avatar asked Apr 24 '19 17:04

Rahul


People also ask

How do I get all the files in a directory dart?

How to list the contents of a directory in Dart. final dir = Directory('path/to/directory'); final List<FileSystemEntity> entities = await dir. list(). toList();

How do I import library darts?

When importing a library file from another package, use the the package: directive to specify the URI of that file. import 'package:utilities/utilities. dart'; When importing a library file from your own package, use a relative path when both files are inside of lib, or when both files are outside of lib.

What is export in dart?

Exports the current Flutter internationalisation files to a singl CSV so that all translations can be worked on in on document. Once translation is complete, you can then use CreateCommand to generate new arb files to use in your Flutter project.


1 Answers

You can create a file in the screens directory and call it all.dart or whatever you like. In this file, you will simply export all of the Dart files in that folder:

export 'homepage.dart';
export 'aboutTheApp.dart';
export 'upcomingEvents.dart';

Now, whenever you want to use any file from that folder, you can just import all.dart or what you called it:

import 'screens/all.dart`;

...

Other than that, there is no possibility to import a directory.

like image 61
creativecreatorormaybenot Avatar answered Sep 23 '22 08:09

creativecreatorormaybenot