Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable relative path auto import in Android Studio in Flutter

I'm using Android Studio for the development of Flutter. I want the auto-imported statements to be imported as in a relative path to the file instead of as an absolute path from the root. I want this thing only for the custom Widgets I'm creating, not for Flutter/Dart internal packages.

Actual

import 'package:stack_app/modules/home/widgets/header.dart';

Expected

import 'widgets/header.dart';

I have seen the setting in Andriod studio, but couldn't find it to customize. Can anyone direct me to some of this IDE plugin/settings where I can change such settings?

like image 906
Yashwardhan Pauranik Avatar asked Aug 26 '20 10:08

Yashwardhan Pauranik


People also ask

How to import flutter and dart in Android Studio?

Before importing this project into the Android Studio we have to install two plugins in the Android Studio, here’s how we do it. Step 1: Open the Android Studio and select Tools from the menu bar and click on SDK Manager. Step 2: In the newly open window click on the plugins and in the search bar search for Flutter and Dart and then install it.

How to auto-import all in Android Studio?

Here, we are going to explain how to auto-import all in Android studio. Here Select Java and then change Insert imports on paste value to All Step 3: Then mark the “Add unambiguous imports on the fly” option as checked. Then click on Apply and Ok.

Should we use relative imports or package imports in flutter?

In Flutter, for importing libraries within our own package's lib directory, should we use relative imports or package import? PREFER relative paths when importing libraries within your own package’s lib directory. whereas Provider package says to always use packages imports : Always use package imports. Ex: import 'package:my_app/my_code.dart';

How do I use flutter auto import?

Run the flutterautoimport command from the Command Palette (⇧⌘P) Automatically finds files, add import line when your execute the Command Palette (⇧⌘P) and select packages. filesToScan - Glob for which files in your workspace to scan, defaults to '**/*. {dart, d}'


2 Answers

There is a workaround for this: Firstly auto import it with absolute path. Then, use the "Convert to a relative import" to make it a relative import.

Example:

enter image description here

Result:

enter image description here

By the way, why do you want to have relative imports? This may be a X-Y problem. For example, by using absolute imports, it is clear where a file is used, by simply search strings like import 'package:sth/your_file.dart'.

like image 144
ch271828n Avatar answered Oct 16 '22 23:10

ch271828n


I also prefer to use relative imports and I usually use the "convert to relative import" function in android studio. Now I have started to see that that option doesn't exist sometimes.

like image 1
Meconer Avatar answered Oct 17 '22 00:10

Meconer