how to make a list observable with getx in flutter

Viewed 5924

I was watching tutorials on using Getx with APIs. after setting a controller for fetching data. we declared a list variable and made it observable(obs). but the list format was deprecated. can you help me how can i do it now.

var products = List<Product>().obs;
4 Answers

You can specify the type of the list easily like this

var products = <Product>[].obs;

Try this

RxList<Product> product = (List<Product>.of([])).obs;

Actually i found the answer for this questions. This is the new way of declaring a list in flutter after the null safety update :

var products = [];

And to make it observable with Getx package we do it like this:

var products = [].obs;

But i dont know how to specify the type of the list in the declaration.

You can create a list simply like:

final listHere = <ListTypeHere>[].obs;

A little bit differently from @Wesley's answer is the final which you can use for sake of efficiency since you will be using the .value.

Related