0

I was just initializing the list, all was woking fine untill i upgraded the flutter as now I'm gettting an error stating as The default 'List' constructor isn't available when null safety is enabled. . I'm using the get and get_storage dependencies.

This is my controller class where I'm getting the error

class TodoController extends GetxController {
  var todos = List<Todo>().obs;

  @override
  void onInit() {
    List storedTodos = GetStorage().read<List>('todos');

    if (!storedTodos.isNull) {
      todos = storedTodos.map((e) => Todo.fromJson(e)).toList().obs;
    }
    ever(todos, (_) {
      GetStorage().write('todos', todos.toList());
    });
    super.onInit();
  }
}

And this is the model class I'm using

class Todo {
  String text;
  bool done;

  Todo({required this.text, this.done = false});

  factory Todo.fromJson(Map<String, dynamic> json) =>
      Todo(text: json['text'], done: json['done']);

  Map<String, dynamic> toJson() => {'text': text, 'done': done};
}```

While initializing the constructor I did get an error stating as ```The parameter 'text' can't have a value of 'null' because of its type, but the implicit default value is 'null'.```

Can i get to know a bit more about this?
Niroop Nife
  • 356
  • 1
  • 5
  • 18

2 Answers2

1

I think you can just get rid of the constructor parenthesis and rearrange like this:

List<Todo> todos = [].obs;

There's a list example in the get docs too.

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61
1

It was deprecated that usage, see the docs

You should use var todos = <Todo>[].obs; instead, and you still keep the Type information

croxx5f
  • 5,163
  • 2
  • 15
  • 36