Home > OS >  Shuffle a list in Flutter
Shuffle a list in Flutter

Time:04-19

I want to shuffle list called "Travel". So, user will see random members of widget every time.

However, when i do final _list = Travel.generateMembersMain().shuffle(); I came error on this line on widget: itemCount: _list.length, and var travel = _list[index]; This expression has a type of 'void' so its value can't be used.

I will be so happy if someone can help.

List Class:

class Travel { String name;
String location; 
String url; 
String placedetails_start; 
String placedetails_end; 
String places_url; 

Travel(this.name,
this.location,
this.url,
this.placedetails_start,
this.placedetails_end,
this.places_url,);

static List<Travel> generateMembersMain() {
return [
   Travel("Place 1", "XXX", "XXXX",
      "XXXXX", "XXXXXX", "XXXXXXX"),
   Travel("Place 2", "XXX", "XXXX",
      "XXXXX", "XXXXXX", "XXXXXXX"),
];  }

Widget:

class Travelblog extends StatelessWidget {
  final _list = Travel.generateMembersMain();
  final _pageCtrl = PageController(viewportFraction: 0.9);

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
        controller: _pageCtrl,
        itemCount: _list.length,
        itemBuilder: (context, index) {
          var travel = _list[index];
          return GestureDetector(
              onTap: () {
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return DetailPage(
                    travel: travel,
                  );
                }));
              },
              child: Stack(
                children: [
                  Padding(
                    padding:
                        const EdgeInsets.only(top: 10, right: 20, bottom: 30),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(5),
                      child: Image.asset(travel.url,
                          width: MediaQuery.of(context).size.width,
                          fit: BoxFit.cover),
                    ),
                  ),
                  Positioned(
                    bottom: 50,
                    left: 15,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Material(
                            color: Colors.transparent,
                            child: Text(
                              travel.location,
                              style:
                                  TextStyle(color: Colors.white, fontSize: 20),
                            )),
                        Material(
                            color: Colors.transparent,
                            child: Text(
                              travel.location,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 35),
                            )),
                      ],
                    ),
                  ),
                  Positioned(
                    bottom: 0,
                    right: 30,
                    child: Container(
                      width: 60,
                      height: 60,
                      decoration: BoxDecoration(
                          color: Colors.deepOrange,
                          borderRadius: BorderRadius.circular(30)),
                      child: Icon(
                        Icons.arrow_forward,
                        color: Colors.white,
                        size: 30,
                      ),
                    ),
                  )
                ],
              ));
        });
  }
}

CodePudding user response:

change generateMembersMain to

static List<Travel> generateMembersMain() {
    var travels = [
      Travel("Place 1", "XXX", "XXXX", "XXXXX", "XXXXXX", "XXXXXXX"),
      Travel("Place 2", "XXX", "XXXX", "XXXXX", "XXXXXX", "XXXXXXX"),
    ];

    travels.shuffle();
    return travels;
  }

Hope helpful to you.

CodePudding user response:

I found the answer, sorry for bothering here. I write to shuffle() inside of widget:

Widget build(BuildContext context) {
_list.shuffle(); //Here
return PageView.builder(
    controller: _pageCtrl,
    itemCount: _list.length,
    itemBuilder: (context, index) {
      var travel = _list[index];

CodePudding user response:

I generally keep in mind these two simple rules when I look for a suitable List method:

  • Some List methods return a value, however, some do not.
  • Also, some of them change the list, but some do not.

For example, the return type of shuffle is void. And it changes the list also.

void shuffle(
   [Random? random]
)

Another example: The return type is bool and it also changes the list (removes the item)

bool remove(
  Object? value
)

Another one: firstWhere Returns the first instance/Element(E below) searched for, however, it has no effect on the list.

E firstWhere(
     bool test(E element),
     {E orElse()?}
)
  • Related