calvindio
calvindio

Reputation: 417

Is there a way to autofill required class / widget properties in vscode / visual studio code flutter?

When constructing a class/widget, usually there are some properties that are required and some that are optional (using @required decorator in the class constructor).

It would be nice if vs code automatically fill in the required keys (eg. child:, onTap:) of these properties instead of just telling an error that "The parameter 'requiredParameter' is required.dart(missing_required_param)".

For example

//example of the class with constructors that we want to use
class Product {
  final String id;
  final String title;
  final String description;
  double price;
  String imageUrl;
  bool isFavorite;
  Product({
    @required this.id,
    @required this.title,
    @required this.description,
    this.price,
    this.imageUrl,
    this.isFavorite = false,
  });
}

//desired behavior of autocomplete / shortcut
Product(
    id: ,
    title: ,
    description: ,
)

Currently, what I do is I manually add the required parameter from the class documentation / vscode popup instructions (eg:

The parameter 'price' is required.dart(missing_required_param)
The parameter 'id' is required.dart(missing_required_param)
The parameter 'title' is required.dart(missing_required_param)

)

Upvotes: 6

Views: 3194

Answers (2)

Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13890

If you are using VSCode, install Dart Data Class Generator

  1. Create a class with properties.
  2. Place your cursor on the first line of the class, the constructor or a field.
  3. Hit CTRL + . to open the quick fix dialog.
  4. Choose Generate constructor.

enter image description here

Upvotes: 3

Shayne Kelly II
Shayne Kelly II

Reputation: 143

Open VS Code settings.json and add the following:

"dart.previewNewCompletionPlaceholders": true

EDIT 2020-01-21:

Thanks William Terrill for your comment mentioning this, this feature is no longer in preview and can be activated with the following command:

"dart.insertArgumentPlaceholders": true

Upvotes: 4

Related Questions