Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize dart code inside web folder

Tags:

dart

dart-pub

According to the Package layout conventions the web folder should contain the following:

HTML, CSS, images, and, heck, probably even some JavaScript. All of that goes into your package’s web directory. You’re free to organize the contents of that to your heart’s content. Go crazy with subdirectories if that makes you happy.

So my web directory looks something like this:

web/data_access
web/model
web/ui
web/ui/navigation
etc.

Now, how to I manage all those import statements. I get a lot of statements like:

import '../../model/my_model.dart';
import '../../data_access/mock_dao.dart';
etc.

I don't like using that many ../ in my imports because this is fragile and I get problems whenever I change anything in my folder structure.

Is there a better way to organize code inside the web folder?

or

Is there another way to do the imports?

like image 454
Marco Jakob Avatar asked Apr 22 '13 12:04

Marco Jakob


People also ask

How do you like to organize the code for Flutter apps do you follow any common conventions?

start from the domain layer and identify the model classes and business logic for manipulating them. create a folder for each model (or group of models) that belong together. within that folder, create the presentation , application , domain , data sub-folders as needed. inside each sub-folder, add all the files you ...

How do I import a Dart file to another folder in Flutter?

Using the path File-> New File creates a new dart file with name nextpage. dart inside the lib folder. Import the package 'package:flutter/material. dart'.


1 Answers

I put almost all of my app's code into lib, to avoid the issues you're seeing. This works even for web components.

See this components from widget.dart: https://github.com/kevmoo/widget.dart/tree/master/lib/components

I usually only put app.dart into web/, which simply pulls in libraries from lib and initializes the app.

See this example of an app that pulls in a pub package that has components: https://github.com/sethladd/catpic-app

This is what my HTML file looks like. Notice the inclusion of packages in the path:

<!DOCTYPE html>
<html>
<head>
  <title>Your life is complete</title>
  <link rel="components" href="packages/catpic/components/cat_pic.html">
  <link rel="components" href="packages/frame/components/frame.html">
  <link rel="stylesheet" href="styles.css">
</head> 
like image 194
Seth Ladd Avatar answered Sep 20 '22 15:09

Seth Ladd