0

I tried to define a class, which contains a List of a specific item-object, which will be populated within Constructor and will also receive new items at a later time:

class Repository {

  final List<Voting> _items = List<Voting>();

  Repository() {
    _items.add(Voting(1, "xyz", 0));
  }

  List<Voting> fetchItems() {
    return _items;
  }

}

However, Flutter is complaining:

The default 'List' constructor isn't available when null safety is enabled.

How to do?

3
  • Change the line final List<Voting> _items = List<Voting>(); to List<Voting> _items = new List<Voting>(); or List<Voting> _items = []; Commented May 24, 2021 at 14:48
  • 1
    Does this answer your question? Why List() constructor is not accessible in Dart's null safety? Commented May 24, 2021 at 14:49
  • use final List<Voting> _items = []; to define an empty list Commented May 24, 2021 at 15:37

2 Answers 2

2

Try this:

final List<Voting> _items = <Voting>[];

instead of

final List<Voting> _items = List<Voting>();
Sign up to request clarification or add additional context in comments.

Comments

0

List() is deprecated with Dart null safety, you can read more about it here.

You should either use [] or List.filled to create a list. For example:

final List<int> items = [];
final items = <int>[];

or

final List<int> items = List.filled(10, 0, growable: true)

4 Comments

Use an example of objects please not int
@LutaayaHuzaifahIdris Replace int with the type of your object.
What of the params, where I see (10, 0, growable: true) , what should be placed there, because it complains that they are for int.
@LutaayaHuzaifahIdris You should obviously be using the type of your object. I don't know what type it is. But you can create some mock data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.