Flutter & Dart Development Tips

Created on: 04 Jul 21 00:01 +0700 by Son Nguyen Hoang in English

Tips on Flutter Development

Recently I decided to create a time management app by using Flutter for personal usage. The reason I choosed Flutter is because it supports cross-platform, and secondly, it is new and I never tried it before, while in the past I learnt Kotlin and Swift before.

Personally, Kotlin appears to be a more simplified and productive version of Java which i am no longer interested in. In contrast, the development and structure of a SwiftUI App is a huge difference, more modern and compact, where things be crafted as Lego-blocks. And Flutter, fortunately, gives me the same vibe when make app on it.

However, doing Flutter Development on Android Studio is sometime troublesome and a lot of configs & prepration must be made. Hence, I Created this tip post to summarize common bugs and solution to fix.

  • This article is under ongoing development hence the content may be updated and changed regularly.

Hot Update 19/5/2023:

  • Create flutter project from terminal command flutter create app_name instead of Android studio! For now , plenty of crazy issues happened to me when creating flutter from Android Studio’s default template!

  1. Error when running flutter doctor --android-licenses from the terminal.
Error Content
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
        at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
        at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
        at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
        at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73)
        at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
        ... 5 more
Solution

Installing Android SDK Command-line Tools (lastest) From SDK Manager.


  1. Error when running an emulator from AVD manager
Error Content
Unable to locate ADB
Solution #1

Uninstall platform-tools (Android SDK Platform-tool), then re-install it.

Solution #2

In Android Studio, go to Project Structure, set Project SDK to a proper path.


  1. Cannot find Database Inspector & Device Explorer

It’s sound crazy but any source I looked when researching about this question seems to be very lack there of. According my own test, the problem happends only when we start a Flutter Project. The Android Studio version I used was 4.2.2, with Android SDK 30. Flutter 2.2.3 and Dart of 2.13.4

Error Content

No database inspector option when search for it in the Menu > View > Tools Window ảnh

Solution

Go to Project Structure > Module > Under the project name, add new module, select Android SDK 30.

UI Image

Reference

https://github.com/flutter/flutter-intellij/issues/4994


  1. Build APK
flutter build apk --split-per-abi

The result is a cluster of apk file. These files often stay in “your-project-folder/build/app/outputs/flutter-apk”

Reference

https://flutter.dev/docs/deployment/android


  1. A warning on top: Dart need to be configured in Android Studio

While the warning suggests you to download Dart. In case you have Flutter in your machine, what you need is configure Flutter in the Editor.

  • Hit File -> Setting -> Languages & Frameworks -> Select Flutter
  • Choose a path for Flutter SDK path
  • Hit apply.

The warning should be gone by now.


  1. Firebase-related Error: Plugin project :firebase_core_web not found. Please update settings.gradle.

Error:

Plugin project :firebase_core_web not found. Please update settings.gradle.
Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01
Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01

This is followed by another long, screwed-up log output:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDexDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
     The number of method references in a .dex file cannot exceed 64K.
     Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

* Try:

Solution:

  • Go to android/app/build.gradle
  • In default config, set minSdkVersion to 23

When this problem occurs, my package versions at the moment (Oct, 2021) are:

pubspect.yaml

version: 1.0.0+1

environment:
  sdk: ">=2.11.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # Firebase
  firebase_core: ^0.4.0
  firebase_auth: ^0.11.1+6
  cloud_firestore: ^0.12.5

  firebase_messaging: ^5.0.2


  1. Your .dart file no longer be regconized by Android Studio IDE

The error occurs randomly and there are very little information about it. I encountered it in 27th of November, 2021. You can refer to the below image to see the problem.

As you can see, there is no highlighting, no formatting and more than that, near the file icon there is no dart symbol which we usually see in normal dart file.

How to solve this problem? Refactor the file to another name! I guess the filename has been conflicted with other library (???), so that this weird behavior occurs.

The second method is to follow this guy’s advice. Image was taken from Stackoverflow, the original link is from:

https://stackoverflow.com/questions/58393704/android-studio-not-recognising-a-file-if-a-specific-name-is-given


  1. Error: Connection closed before full header was received when running flutter pub get

A crazy error that had existed for a very long time.


Running "flutter pub get" in test_flutter...
Resolving dependencies... (1.1s)
Connection closed before full header was received
pub get failed
command: "C:\tools\flutter\bin\cache\dart-sdk\bin\dart __deprecated_pub --color --directory . get --example"
pub env: {
  "FLUTTER_ROOT": "C:\tools\flutter",
  "PUB_ENVIRONMENT": "flutter_cli:get",
  "PUB_CACHE": "C:\Users\sonng\AppData\Local\Pub\Cache",
}
exit code: 69

If the problem keeps persist, delete folder .git in the repo then run git init to initialize a new one.

Resolve: Inititialize the project with git (don’t ask me, I dunno why. Ask this guy instead: https://stackoverflow.com/questions/75233604/how-do-i-resolve-these-depreciation-errors-in-flutter)

Another way to solve this problem is to simply run “flutter upgrade”. I encountered this bug on an old laptop in which flutter is not up-to-date. run flutter upgrade then things go smooth again.

  1. Set Brave as Chrome_Executable variable in Windows

Running flutter doctor gives you warning if Chrome is not your installed browser. As a Brave user, I make a new System Variables (or user variable? I created both!).

Variable Name: CHROME_EXECUTABLE
Value: C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe // example

  1. Gradle and Java Incompability

Error happends when start a new project

Flutter & Android: Your build is currently configured to use Java 19.0.2 and Gradle 7.0.2

Visit Gradle Scripts -> gradle-wrapper.properties. Set the new link to download the new gradle.

Check the gradle and java compability and select the correct version. Also, here is the link to a similar question (stackoverflow): https://stackoverflow.com/questions/70890575/flutter-android-your-build-is-currently-configured-to-use-java-17-0-2-and-gra

  1. flutter do not create default pubspec.yaml file when making new project

Error happends for me because my flutter package was way behind remote origin

Update the flutter package! The quickiest way would be to use git then pull the project from remote.

  1. Unwanted Feature: Android studio hide folders that contain one sub-folder only.

Project -> Setting -> Tree Appearance -> untick Compack Middle Package

  1. “Build Error”:
Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module @fb04536

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 27s
Exception: Gradle task assembleDebug failed with exit code 1

Resolve:

For me upgrading android/build.gradle

from classpath 'com.android.tools.build:gradle:4.2.2'

to classpath 'com.android.tools.build:gradle:7.2.1'

WORKED 🎉

Origin Source: https://github.com/fluttercommunity/flutter_workmanager/issues/287

  1. Can not run the app from IDE:
Flutter: Gradle build failed to produce an .apk file. It's likely that this file was generated under <app_root>\build, but the tool couldn't find it

This can be caused because of build-flavor. Check the Edit Configuration in the running app button (Edit Configration often be inside the Run button) and set the build flavor to correct value. Mine was dev

Back To Top