if I type autorun inside the code, vscode suggests me to auto import it from mobx:
I want to define some custom names for that for example if I type _ somewhere in the code, I want vscode to suggest me to import lodash
or if type PropTypes I want vscode to suggest auto import PropTypes from 'prop-types'
Is this possible?
I haven't written any vscode extensions yet, but I can take a swing at it if that's what it takes to complete this functionality
Thanks in advance for any help and guidance!
There's no support for this built into VSCode currently. Basically you're looking to define a list of names/aliases for default exports (i.e import MyCustomName from 'lodash'). Cool idea! I think you'd have to write a pretty low-level VSCode extension to accomplish this might end up being limited by the TypeScript compiler itself. Good luck!
Best I could do is declaring it on a file and importing it from there:
tailwind.ts
export { default as tailwind } from "tailwind.macro";
someFile.ts
import tailwind from "~/tailwind";
Related
Is there an option to disable "explicit" import suggestions in VSCode, like shown in the picture below? I find these visually displeasing because they add extra unnecessary lines. These are Haskell imports.
Problem Statement
Currently, bringing up the Android Studio Quick Fix menu (Opt/Alt+Enter keyboard shortcut) on AppLocalizations does not suggest importing the generated file.
The AppLocalizations class lives at .dart_tool/flutter_gen/gen_l10n/app_localizations.dart, which is also a Git-ignored directory.
As a result I have to type the import statement by hand. Other Flutter class names commonly suggest importing a relevant file via the Quick Fix menu.
What Is Expected
I am expecting the Quick Fix menu to suggest importing the generated AppLocalizations file. When I click on the import suggestion, it should insert it alongside my other import statements at the top of the file.
Question
How can I get the import suggestion to appear in the Quick Fix menu for AppLocalizations? Do I need to help the Dart analyzer "know about" the generated files inside the .dart_tool directory? Can I include the generated file in my "Project" while still Git-ignoring it? Do I somehow need to link to it in my pubspec file?
Screenshot of Quick Fix menu missing import suggestion
I didn't find a solution for an automatic suggestion.
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Just add this import manually. Note: it still at first didn't recognise AppLocalizations.delegate, so I removed this line of code and started typing again. Now it worked.
EDIT:
Since you need to import AppLocalizations at a lot of places, for me the workaround was to create a class that gets me an instance of this class.
I have created a common.dart class with a function:
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
AppLocalizations? getApplocalizations(BuildContext context){
return AppLocalizations.of(context);
}
And I call translations from my widgets like this:
title: Text(getApplocalizations(context)?.pageHomeTitle("Test") ?? ""),
This way you add an import manually only at one place.
Flutter needs to generate the file with code from json file, you can do it by running e.g. flutter pub get or flutter pub upgrade or flutter build ... or flutter run
So, here is the problem:
Whenever I am coding with Flutter and Dart, and I use a feature from another file, when I press Tab, VS Code will auto-import the file for me.
The thing is, VS Code will use the package import syntax, like import 'package:<project_name>/<file_path>';. But I prefer relative import syntax.
So, is there a setting I can change in VS Code for Dart to configure it to use relative imports by default?
Thanks!
While importing, you can find both option by tapping on yellow bulb or pressing ctrl+.
Another handy thing, using dart-import extension on Vs-code.
Personally, I am using dart-import extension. There is an extension setting you can turn on to modify imports on save. So, you can have a consistent import structure without needing to choose every single time.
There is a recommendation in effective dart guideline for relative path import usage.
Recently the auto import feature for packages and widgets stopped from working, How can I solve it and back it to work? Here an example for import the material flutter package.. in the past when I used to click on fast solve button it was give me an option to import it automatically .Flutter auto import does not work
import material.dart to get the rest of the widget working.
I've had to deal with that for a month or so, and I only managed to solve it a few days ago.
If your problem is anything like mine then it's probably due to one of your settings.
The brute force solution I found is to completely clear your settings.json file and set your preferences again one by one.
Only add those you actually need. I had a lot of them which didn't change much or anything I could notice.
You could also comment out each line one by one to try to find out what exact setting is causing the issue but that's more tedious than doing it all over again.
Every time I want to add a widget by selecting it on the autocomplete list from VS Code, let it be a Container, Column, etc., the full path is re-imported. Sometimes the autocomplete list will also show multiple lines for the same widget. import 'package:flutter/material.dart';' is already included on the file.
e.g.
import '../../../../../flutter_linux_v1.0.0-stable/flutter/packages/flutter/lib/src/widgets/framework.dart';
import '../../../../../flutter_linux_v1.0.0-stable/flutter/packages/flutter/lib/src/widgets/container.dart';
Image here: screenshot of autocomplete
It goes away if I run Flutter clean but comes right back. Any ideas to make this go away?
Which extensions are you using? Might there might be some extensions which is overriding flutters default extension behaviour.
You can run code --list-extensions from command line to get all extension installed. You can post it here so that me or someone else can help you out.