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?

delete
  • 18,144
  • 15
  • 48
  • 79
  • Change the line ``final List _items = List();`` to ``List _items = new List();`` or ``List _items = [];`` – OMi Shah May 24 '21 at 14:48
  • 1
    Does this answer your question? [Why List() constructor is not accessible in Dart's null safety?](https://stackoverflow.com/questions/63451506/why-list-constructor-is-not-accessible-in-darts-null-safety) – OMi Shah May 24 '21 at 14:49
  • use `final List _items = [];` to define an empty list – Mohammed Alfateh May 24 '21 at 15:37

2 Answers2

2

Try this:

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

instead of

final List<Voting> _items = List<Voting>();
kforjan
  • 584
  • 5
  • 15
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)
iDecode
  • 22,623
  • 19
  • 99
  • 186