Flutter - Other

Easier Assets Management

Do

Image.asset(AppAssets.appLogo);


Don't

Image.asset("assets/images/app_logo.png");


Conclusions: Managing assets can be very hard. If you want to use an image several times in your app, you have to specify the path again. But there is a much easier solution for this. Create an App Assets class where all your app assets are stored. Now you can easily call your assets with AppAssets.appLogo.

Enable code obfuscation

Update your pubspec.yaml to enable obfuscation.


environment:
  sdk: ">=2.12.0 <3.0.0"

flutter:
  obfuscate: true
  split-debug-info: /path/to/debug-info

Entry name *** collided

Add android.useNewApkCreator=false to android\local.properties.

execution failed for task ':gradle:compilegroovy'. > bug!

Open android folder of your project and the gradle folder and then open gradle-wrapper.properties file you need to update distributionUrl in gradle-wrapper.properties file.


Your Project Old distributionUrl is


distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip


Just change the version of distributionUrl with new distributionUrl


distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip (copy this and replace with old distributionurl)


After paste the new distributionUrl in gradle-wrapper.properties file open terminal of your IDE type cd android and then paste this ./gradlew signingReport.


It take 10 to 15 minutes your error will solved and you can easily generate SHA1 key for your project.

File and Code Templates (Android Studio)

Open Settings -> File and Code Templates -> Create Template


Now apply the following setting in the template.

  • Add name of the template.
  • Set extension to dart
  • File name to ${NAME}


Stateless Widget

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")
#end

#set( $list = ${NAME.split("_")} )
#set( $className = "" )
#set( $item = "" )

#foreach( $item in $list )
    #set($str = ${item.substring(0,1).toUpperCase()} )
    #set($str2 = ${item.substring(1).toLowerCase()} )
    #set($className = "${className}${str}${str2}" )
 #end


import "package:flutter/material.dart";

class $className extends StatelessWidget {
  const $className({super.key});

  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}


Stateful Widget

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")
#end

#set( $list = ${NAME.split("_")} )
#set( $className = "" )
#set( $item = "" )

#foreach( $item in $list )
    #set($str = ${item.substring(0,1).toUpperCase()} )
    #set($str2 = ${item.substring(1).toLowerCase()} )
    #set($className = "${className}${str}${str2}" )
 #end


import "package:flutter/material.dart";

class $className extends StatefulWidget {
  const $className({super.key});

  @override
  State<$className> createState() => _${className}State();
}

class _${className}State extends State<$className> {
  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}


Empty Class

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")
#end

#set( $list = ${NAME.split("_")} )
#set( $className = "" )
#set( $item = "" )

#foreach( $item in $list )
    #set($str = ${item.substring(0,1).toUpperCase()} )
    #set($str2 = ${item.substring(1).toLowerCase()} )
    #set($className = "${className}${str}${str2}" )
 #end



class $className{
  
}

flutter doctor --android-licenses gives a java error

  1. Right-click on "This PC" or "My Computer" and select Properties.
  2. Click on Advanced system settings.
  3. Click on the Environment Variables button.
  4. Under System variables, click New.
  5. Enter SKIP_JDK_VERSION_CHECK as the variable name and true as the variable value.
  6. Click OK to close each dialog box.
  7. Restart your Command Prompt or PowerShell to apply the changes.


By following these steps, you can bypass the Java version check when running flutter doctor --android-licenses on Windows.

Flutter Terminal Commands

  1. flutter pub get - Get packages in a Flutter project.
  2. flutter create <app_name> - Create a new Flutter project.
  3. flutter analyze - Analyze the project's dart code.
  4. flutter clean - Delete the build/ and.dart tool/ directories.
  5. flutter devices - List all connected devices.
  6. flutter test - Run Flutter unit tests for the current project.
  7. flutter run - Run your Flutter app on an attached device.
  8. flutter build apk - Build an Android APK file from your app.
  9. flutter upgrade - Upgrade your copy of Flutter.
  10. flutter doctor - Show information about the installed tooling.

Folder Structure

Do

lib
--core
----services
------api_service.dart
----shared_widgets
------custom_textfield_widget.dart
----utils
------utils.dart
--features
----authentication
------screens
--------login_screen.dart
--------register_screen.dart
----home
------screens
--------home_screen.dart
------widgets
--------product_widget.dart
--main.dart


Don't

lib
--api_service.dart
--custom_textfield_widget.dart
--home_screen.dart
--login_screen.dart
--main.dart
--product_widget.dart
--register_screen.dart
--utils.dart


Conclusion: Making it easier to locate and organize files.

Note: If you use state management you can add it in every feature. Example: features/home/bloc.

Having trouble displaying splashes using an InkWell

Use an Ink widget ! The Ink widget draws on the same widget that InkWell does, so the splash appears.


class InkWellCard extends StatelessWidget(
  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 3.0,
      child: Ink(
        child: InkWell (
          onTap:() => print("Ordering..."),
          child: Text("Order Bageis'),
        )
      )
    );
  }
}

How to build signed apk from Android Studio ?

  1. Go Tools > Flutter > Open for Editing in Android Studio and select New Window option.
  2. Wait for while until project synchronization.
  3. Go to Build > Generate Signed Bundle/APK.
  4. Select Android App Bundle or APK option and click Next.
  5. Select Create new.. option to generate new Signed Key if you release your app first time.
  6. Fill all Options.
  7. Click OK then click Next.
  8. Build Variants.
  9. Wait for a while until Gradle Build Running process complete.


Options

Key store Path - Path where your key store file .jks file stored.
Key store password - Enter password. e.g. 123456
Key alias - Enter Key alias e.g. key
Key Password - Enter Key password (Choose different password than Key store password) e.g. 123456
Validity(years) - Keep it as it is or change as per your requirements.
Certificate - Fill certificate information (Not all fields are mandatory)


Variants

Android App Bundle

  1. Select release and click Finish.


APK -

  1. Select profile and checked V1 and V2.
  2. Click Finish.
  3. Repeat the step by select release after the profile apk generated.

How to Change Package Name ?

For Android App Name

Change the label name in your android\app\src\main\AndroidManifest.xml file.

<application
  android:name = "io.flutter.app.FlutterApplication"
  android:label = "TheNameOfYourApp"


For Package Name

Change the label name in your android\app\src\main\AndroidManifest.xml file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="your.package.name">


Also in your android\app\src\build.gradle file inside app folder.

defaultConfig {
 applicationId "your.package.name"
 minSdkVersion 16
 targetSdkVersion 29
 versionCode 1
 versionName "1.0"
 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}


Finally, change the package in your android\app\src\main\kotlin\your.package.name\MainActivity.java class.

package your.package.name;


Change the directory name.

  1. Change From android\app\src\main\java\com\example\name to android\app\src\main\java\your\package\name.

How to Change the Android Notification Icon/Status Bar Icon for Push-notification

Just add a meta-data inside tag in your manifest file.


<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />

<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />