1

i implemented listView in flutter and it shows product count=5 , but i wanted these 5 items to be generated randomly , is there a way to do it? thanks in advance ps: i tried code depending on answer below but it gives me error count!=null

Expanded(
                      child: SizedBox(
                          height: 210.0,
                          child: FutureBuilder(
                            future: httpService.getProducts(),
                            builder:
                                (BuildContext context, AsyncSnapshot snapshot) {
                              if (snapshot.data == null) {
                                return Container(
                                  child: Center(
                                    child: Text('Loading...'),
                                  ),
                                );
                              } else if (snapshot.data.length == 0) {
                                return Container(
                                  child: Center(
                                    child: Center(
                                      child: Text('No offers'),
                                    ),
                                  ),
                                );
                              } else {
                            
                               var rndItems  = snapshot.data.shuffle();
                                         return ListView.separated(
                                    separatorBuilder:
                                        (BuildContext context, int index) {
                                      return SizedBox(height: 3);
                                    },
                                    scrollDirection: Axis.horizontal,
                                    shrinkWrap: true,
                                    itemCount: rndItems ,
                                    itemBuilder: (ctx, i) => (PdtItem(
                                        title: snapshot.data[i].title,
                                        imgUrl: snapshot.data[i].imgUrl,
                                        price: snapshot.data[i].price,
                                        pdt2: snapshot.data[i])),
                                  );

4
  • Do you mean you want the snapshot.data to be listed in a random order? Commented Nov 10, 2020 at 20:54
  • yes exactly , i want itemcount equals random items from snapshot.data Commented Nov 10, 2020 at 20:59
  • Try itemCount: Random.nextInt(snapshot.data.length) Commented Nov 10, 2020 at 21:12
  • it shows an error, Instance member 'nextInt' can't be accessed using static access. Commented Nov 10, 2020 at 21:17

1 Answer 1

1

If you want the items from snapshot.data to be listed in a random order then you may shuffle the data as follows :

....
snapshot.data.shuffle();
....

If you want to display random number of items everytime then

....
import 'dart:math';
....
var rng = new Random();
var rndItems = rng.nextInt(snapshot.data.length);
....

.....                                    
                    scrollDirectionn: Axis.horizontal,
                    shrinkWrap: true,
                    itemCount: rndItems,                                                          
                    itemBuilderr: (ctx, i) => (PdtItem(
.....
Sign up to request clarification or add additional context in comments.

12 Comments

thanks for ur answer, i tried the second one and it shows me error, The instance member 'rng' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression
Did you add the line >>> import 'dart:math'; << at the top of the code?
I also tried itemCount:snapshot.data.shuffle(), and it didn't work ,error says: itemCount!=null
If you are adding the import then add the line >>> var rng = new Random(); var rndItems = rng.nextInt(snapshot.data.length); <<< after snapshot.hasdata. Then make sure to use >>> itemCount: rndItems, <<<
i updated my question cuz these didn't work too, if u please can help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.