Atmane AYOUB
Atmane AYOUB

Reputation: 91

how to make a list observable with getx in flutter

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;

Upvotes: 7

Views: 19587

Answers (5)

Rahul Raj
Rahul Raj

Reputation: 1503

Simply you can create observable model data list like

  List<SampleItem> sampleList = <SampleItem>[].obs;

Upvotes: 2

Raphael Souza
Raphael Souza

Reputation: 107

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.

Upvotes: 0

Wesley
Wesley

Reputation: 194

You can specify the type of the list easily like this

var products = <Product>[].obs;

Upvotes: 14

Dhruvan Bhalara
Dhruvan Bhalara

Reputation: 257

Try this

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

Upvotes: 1

Atmane AYOUB
Atmane AYOUB

Reputation: 91

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.

Upvotes: 1

Related Questions