Enable relative path auto import in Android Studio in Flutter

Viewed 1437

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?

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'.

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.

Related