how to make a list observable with getx in flutter
Asked Answered
O

5

7

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;
Oculomotor answered 29/7, 2021 at 23:22 Comment(0)
U
14

You can specify the type of the list easily like this

var products = <Product>[].obs;
Underarm answered 19/8, 2021 at 17:52 Comment(0)
O
2

Simply you can create observable model data list like

  List<SampleItem> sampleList = <SampleItem>[].obs;
Oldest answered 12/4, 2023 at 9:15 Comment(0)
C
1

Try this

RxList<Product> product = (List<Product>.of([])).obs;
Carboniferous answered 30/7, 2021 at 5:7 Comment(1)
@AtmaneAYOUB I have updated my answer please check and let me know if it's working or notCarboniferous
O
1

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.

Oculomotor answered 1/8, 2021 at 0:7 Comment(0)
M
0

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.

Myriad answered 25/8, 2021 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.