Unable to create desktop application support from existing project in flutter - flutter

I am creating a desktop application from an existing app.
The command given in documentation is :
flutter create .
Running this command in the root of the project directory I am getting this error
"MyApp" is not a valid Dart package name.
Note: MyApp is name of my project

Flutter only allows project names that are lower case letters and may contain an underscore or a number. This is actually a convention from dart packages. Check here for the prescribed convention. It is likely that your folder name is used by flutter as its project name. You can try to overcome this by changing the folder name. But please do keep a backup before doing flutter create on an existing project. For more details on possible options check this post as well.

write app name in lowercase
flutter create myapp

Related

flutter: Target of URI doesn't exist

When I try to open the flutter project of my developer, I get hundreds of errors, that he does not find multiple packages for instance. Packages that consist with the name of my app, so they are probably custom made. He says I should have everything and that it gives errors because my flutter version is newer than the one that the project recognises in vsc. Is this correct? And if so, how can I get these packages?
''''
Target of URI doesn't exist: 'package:fimber/fimber_base.dart'.
Try creating the file referenced by the URI, or Try using a URI for a file that does
exist.dart(uri_does_not_exist)
run flutter clean and then flutter pub get , As far as I assume, related packages are not in the app, so you have to get them via pub get.
Try to write where the Dart file is located in the import section, in a case of sth like this.
Target of URI doesn't exist: 'package:fimber/fimber_base.dart'.
Try creating the file referenced by the URI, or try using a URI for a file that does:
exist.dart(uri_does_not_exist)
Try to delete the import URI to this fimber_base.dart or, if you are using the Android Studio as your IDE, press alt+enter, it should give the library to import.

How to create/provide multiple example apps for a flutter plugin?

Flutter seems to have a very strict/rigid structure for plugins with example folder and all contents inside that folder.
I want to provide multiple examples with my plugin. Something like examples folder and then examples/demo1 and examples/demo2 as two different app examples.
I tried doing this but flutter run or pub get command breaks with this change. it's gets stuck with below error which wasn't thrown with exact same code in previous structure before change. Also my app actually follow embedding v2 code so this error is completely false too.
The plugin `<MY PLUGIN>` requires your app to be migrated to the Android embedding v2. Follow the steps on
https://flutter.dev/go/android-project-migration and re-run this command.
Somehow is it expecting that there should be only one example and that too with example folder only ?
Can someone help, and if possible point me to a plugin project where it's using multiple examples ?
After trying multiple ways to deal with the situation,
I ended up with a good enough solution.
I moved entire flutter repo inside an sdk directory, and then Introduced a samples folder at root level which can contain multiple sample applications.
sdk itself has a default sample app under example folder which I kept so sdk can ship with one example project.
Final structure look like below,
- Root
- sdk
- Flutter plugin project (like android/example/ios/lib directory etc.)
- samples
- sampleOne
- sampleTwo
I then mentioned relative path to sdk for sampleOne or sampleTwo inside their pubspec.yaml like path: ../../sdk/
When I want to open sampleOne in AndroidStudio, I import sampleOne directory and it works like charm. Make sure you don't import entire samples directory or sampleOne/Android.
For any regular Flutter commands for plugin, I run them inside sdk directory and everything works fine as expected. So I would run publish or pub get inside root/sdk directory

How come "flutter create ." adds desktop support?

I created a Flutter project within Android Studio, and then later found out that Flutter supports desktop. After searching the web, I did things like switching to "dev", adding config for desktop, etc. But when I tried to ran it, I got "No Windows desktop project configured", even though I enabled the configuration.
The documentation page had
To add desktop support to an existing Flutter project, run the following command in a terminal from the root project directory:
flutter create .
This adds the necessary desktop files and directories to your existing Flutter project.
But what is the meaning of .? How come that . means "adding desktop support"?
The command to add desktop support are those one:
flutter config --enable-windows-desktop
flutter config --enable-macos-desktop
flutter config --enable-linux-desktop
Once you used them, you change your flutter config.
Then, the usage of flutter create . simply mean "Create a new flutter project in my current folder".
If you're already in a Flutter project, it will just "recreate" your project by adding the new support for Desktop since you specify in your flutter config.
flutter config --enable-[windows,linux,macos]-desktop
flutter create .
if there is some error with second command, remove - sign from parent folder name, and run this command:
flutter create --org com.example.myapp .
Run this command it works fine for me.
run
flutter config --enable-windows-desktop
then run
flutter create .
if flutter create . generate error, probably there is - in your root project directory.
for example it will generate error if your project is like below
Alewa-Stock
to work it fine rename your project directory like below
alewa_stock
then run the following command
flutter create --platforms=windows --org=com.alewa.store .
*
NB: Please change the name according to your preference.
flutter create --platforms=windows,macos,linux .
you can use this line instead

I cannot initialize Flutter for web after adding flavors to my project

I have a Flutter project that currently builds for iOS and Android. I created the project around Flutter v1.9 or so. Since creating the project, I have added flavor support to my project (dev, prod) via the guide found here
However, after having done this, it doesn't seem like I am able to add web support to my project. Following Flutter's official instructions to enable web for a pre-existing project, I try to run flutter create . in the root directory of the project. When I do this I get the output:
The Xcode project defines schemes: dev, prod You must specify a --flavor option to select one of the available schemes.
So I try to run: flutter create . --flavor=dev and get Multiple output directories specified. Try moving --flavor=dev to be immediately following create
Soooo, I try: flutter create --flavor=dev . and get Could not find an option named "flavor".
Does anyone know how I can initialize web for this project? Thanks.
You need to do the following:
Goto root folder and rename manually ios folder temporarily to something like ios1
Goto terminal and make sure you are standing in root, then run this line:
flutter create --platforms=web .
Return to project folder and rename back manually ios1 to ios
Done

Changing the project name

Is it possible changing the project name of a Flutter project? With project name I mean the name you provide when creating a flutter project flutter create name.
That depends on what you are trying to achieve. If you want to change the name of the app which is displayed in the mobile phones menu together with the app icon, you have to change the android:label in the android/app/src/main/AndroidManifest.xml (Code Sample 1) and the CFBundleName in the ios/Runner/Info.plist (Code Sample 2).
Last time I did this it took me ages to find out, why the name of the app was not changed in the list of currently running apps on Android. For that you also need to change the title of your MaterialApp (Code Sample 3).
For renaming it everywhere I would suggest to use search and replace in the project. If you do this, you have to choose a name without spaces or special characters. A project name 'new project' in the pubspec.yaml for example will break the build.
Code Sample 1:
<application
android:name="io.flutter.app.FlutterApplication"
android:label="New Name"
android:icon="#mipmap/ic_launcher">
Code Sample 2:
<key>CFBundleName</key>
<string>New Name</string>
Code Sample 3:
return new MaterialApp(
title: 'New Name'
...);
To change project name , you need to change it for all platforms .
You can use rename package to make it easy .
Run this command inside your flutter project root
flutter pub global run rename --appname "Application Name"
You can also specify the platform you want to apply the change
flutter pub global run rename --appname yourappname -t macOS // support also ios and android
or
Edit AndroidManifest.xml for Android and info.plist for iOS
Do a flutter clean and restart your application if you have a problem.
You can let flutter do this for you:
Using Terminal:
cd /path/to/existing/flutter_project
flutter create --project-name newprojname --org com.yourorgdomain .
Next change the name on line 1 of pubspec.yaml
Trash all generated folders to remove all references of the old project name.
NB - Some import statements will need to have their package references
updated manually to the new name. Review the project structure (in
your IDE) for any references to the old project name inside of IDE
generated files.
Recreate the trashed generated files by using flutter create . (including the space and period at the end) from within the project folder.
Now run flutter clean then debug as normal
Gotchas
You should expect that the project name still resides in all the generated folders for the various environments and in comments/strings. You can use a tool to search all files in the project or folder structure (e.g Cmd + Shift + F in Android Studio or ag {the_silver_searcher in Homebrew}) to find the old name and trash any generated folders including ios, windows, linux. After trashing the generated folders remember to re-create them by running flutter create .
To add to Rainer's answer, you may also have to change the com.example.myprojectname file under android/app/build.gradle
Also for the com.example.myprojectname, do not use underscores (ie:
com.example.my_project_name)
You may also have to find and replace the com.example.myproject name in the files project.pbxproj and MainActivity.java.
If you want to change the project name, simply change it in all these files.
build.gradle (app)
AndroidManifest.xml
MainActivity.java
Rebuild your app and you should be good to go.
1-flutter clean
2- Search and replace all OLD_PROJECT_NAME with NEW_PROJECT_NAME
3- Search for OLD_PROJECT_NAME in the project folder, and replace them with NEW_PROJECT_NAME one by one. Some of them are in the text files, make sure that you have changed them too.
If you are using Android Studio, follow these steps:
Right-click your project in the Project window (Alt+1 to show).
Click Refactor > Rename.
Enter new name.
This will rename the project's name on all required places except in the test directory.
Steps to rename your project: (For Editors that has search & replace across project feature)
Use VSCode's any other editor's search and replaceAll across entire project feature. But always turn on the match cases (for vscode: located on right side).
You have to do this two time as for the first you have to replace com.orgname.currentname with com.orgname.newname then
currentname with newname.
Rename the folders inside /android/app/src/main/kotlin/com/orgname/currentname to new /android/app/src/main/kotlin/com/orgname/newname.
If you're using only Java with flutter than just replace the kotlin with java.
rename the currentname.iml of your root project folder to newname.iml
Ctrl+Shift+R then Replace for one by one replace
or Replace All for all at a time. Be cautious , if your project name is similar with other variable or class name then don't do that. Before doing that check twice.
If you are using Android Studio, from the Menu > Edit > Find > Replace in files > Enter old and new name...
If you want to change the project name, simply change it in all these files.
build.gradle (app)
AndroidManifest.xml
MainActivity.java
Rebuild your app and you should be good to go.