-1

I want to use a List<Widget> (A list of widgets) I'm very new to flutter and I found this code where I am learning flutter from and apparently this code will not work in flutter in 2021. Can null safety be disabled? or What can I do?

List<Widget> _renderFacts(Location location) {
    var result = List<Widget>();

Here's the error I get

The default 'List' Constructor isn't available when null safety is enabled. Try using a list literal[], list.filled or list.generate

double-beep
  • 5,031
  • 17
  • 33
  • 41
vishvak
  • 31
  • 2
  • 5
  • 1
    Does this answer your question? [The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'](https://stackoverflow.com/questions/63451506/the-default-list-constructor-isnt-available-when-null-safety-is-enabled-try) – iDecode Aug 26 '22 at 21:41

2 Answers2

3

You should also do following.

List<Widget> data = List<Widget>.empty(growable: true);
Nilesh Senta
  • 5,236
  • 2
  • 19
  • 27
2

Yes null safety can be disabled but it is certainly the worse thing to do!

Initialising an empty list of widgets is straightforward. As the error you got suggested, just use literal [];

List<Widget> myListOfWidgets = [];
RemDalm
  • 119
  • 2