Which package should the "@required" annotation be imported from?

Asked

Viewed 244 times

3

I need to put the note @required in a builder:

class Pacote {
  final int id;
  final int nCodigos;
  final String tabela;

  Pacote({@required this.id, @required this.nCodigos, @required this.tabela});

  // ...
}

Only that there was no import in this specific file. Thus, gave error that the @required was not a known element:

Undefined name 'required' used as an Annotation.
Try Defining the name or importing it from Another library.

Vscode suggested me to import from one of the following places:

  • package:flutter/cupetino
  • package:flutter/widgets
  • package:flutter/material
  • package:flutter/foundation

cupertino and material are interface styles as I recall, while widgets is related to visual elements. And this particular piece of code is just a business class, without any knowledge or use on screen.

So where should be the most appropriate place to care and have access to this note, @required? It is one of listed or another separate package?

2 answers

2


The @required is an annotation used by Dartanalyser which is provided through meta.

As you will not use visual components, you can import it directly from foundation

import 'package:flutter/foundation.dart';

According to this reply, this annotation was added @required direct to the SDK through this package foundation.dart

In this other link here Required we can see that this annotation comes from the Meta package.

The package foundation.dart has the lowest-level utility features, classes and functions used by all other layers of the Flutter structure.

So I think it’s a good thing to actually pull the reference to @required from her.

Note: If your class is to be used in a mixed project without necessarily just Flutter, then it is worth pulling the separate package meta.dart which is imported into your pubscpec.yaml

2

Really very strange Intellisense suggest these imports.
If you leave the cursor over the @required you’ll see when in fact, the right thing would be:

inserir a descrição da imagem aqui

import 'package:meta/meta.dart';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.