How do you share code between 2 Flutter projects? - flutter

With Flutter now supporting web and mobile app development, what's the best practice to avoid rewriting code all the time? Say you have a few classes / functions living in your mobile app project that'd you'd like to reuse for your web project, is importing these files the way to go? If so, can you do that across multiple projects?

Here is the approach I found: using packages.
You can create a package with the following terminal command:
flutter create --template=package myPackageName
then simply create whatever class, function, etc. you want to share across your projects in the package and import them in your other Flutter projects.
Here is an example: https://youtu.be/MJO695IE-EA

Related

How to practice Dart projects without using Flutter?

I'm learning Dart since I soon want to use Futter for different personal projects, but now that I've gone through this interesting video, I'd like to develop small projects directly using the language and libraries that are in puv.deb.
So far I have only found this exercise which I found very interesting, but all the other projects I find always directly use Flutter. I have also already managed to do the Codelab and other exercises on the Dart website.
What types of projects could you migrate from other languages to try to implement in Dart?
I would appreciate links to websites or videos where they develop projects in Dart

What is the differences between parse_server_sdk and parse_server?

I'm working with Parse Server, my task is to build a flutter application that using Parse SDK to communicate with the Parse Server. You can see the Parse SDK here, these are two packages, one is for Flutter, and another is for Dart. I don't know the difference between these two, and which one I should use.
If you create a Flutter app you should use Parse-SDK-Flutter package. In Migration Guide plugin developers provide this reason for moving from Dart to separate package:
This was done in order to provide a dart package for the parse-server, while keeping maintenance simple. You can find both packages in the package directory..

How to import flutter module to a flutter project?

I have a flutter module project and I want to import it into another existing flutter project, how can I do this?
I think this may not be possible since there will be two main function and don't know how they communicate.
Are there any suggestion so I can combine the these two things? I want to open the module app in another flutter app.
Flutter yet to support this feature out of the box, see issue #64542 for more details.
Since the feature request that was mentioned in above is still open, the best solution that I can think of is by merging another application to your existing flutter application by importing the application you want to merge as a module. Check also this SO post, where this kind of scenario was explained.

Using package that is dependency of another local package

Let's say I have few flutter apps and I want all of them to load common local dependency(package) via path.
Like this:
The reasoning behind is straightforward - to have shared classes/helpers that used by all or most of flutter apps.
Now the core_flutter package have it's own packages as well.
Like this for example:
Now the question. Is it correct to use core_flutter dependencies inside apps that depend on it? Because it's kind of desired behaviour to share core_flutter packages across apps that depend on it.
For example if I need to use provider in my app_one that depends on flutter_core. Is it fine to use it without adding provider into app_one pubspec.
I've tested it already and it work.
The question though is it good practice / reliable.
The only issue right now I've noticed thad IDE won't autocomplete classes and imports in app_one that part of dependencies of core_flutter.

How to organize dart files inside packages in flutter

I'm new at programming using flutter and I'd like to develop a small project containing a few screens: Login, Home, Settings, User, PurchaseHistory, etc.
I need to organize the code inside packages so that it can readable easily.
If I develop an Android App, I'd create some packages: model, activity, fragment, util, etc. If I create a LoginActivty, I'd put it inside activity package. If I create a User model, I'd put it inside model package. And so on.
So If I develop a flutter project, where am I supposed to put all of the files I create so far?
For now I've created only model package.
First of all, in Flutter we don't deal with activities or fragments directly, that is a naming convention from Android itself.
There are many options to architect your app and organize your folders. I wouldn't say that there is a holy grail solution. So you have to try some of them and see the best fit for you.
At the end of this article, I show an option to a folder structure when working with flavors, like this:
But there are plenty of others, so I recommend you to see how some of the GitHub projects are organized, a good way to start is having a look at the projects from this repository. Especially the 'Open Source Apps' section.
I'm currently working on a project that follows an approach like you described.
I don't know if it's the better structure, but it works really nice for me.
\lib
\-model
\-api
\-bloc
\-widgets (commom components)
\-exceptions
\-config (config classes/files like routes, theme, specific settings for each environment - dev, production, test)
\-views
\-login
\-home
\-user_profile
\-...
\-main.dart
----- EDITED -----
After work for almost a year with Flutter, I've tested some different structures, and there's one in particular that's really nice and provides an amazing organization...
Take a look at slidy, the following image represents the kind of organization it provides. More details in the package description.