2

I created a dart file with the list looks like

class Happylist {
  String imageUrl;

  String url;
  String name;

  Happylist({
    this.imageUrl,
    this.url,
    this.name,
  });
}

List<Happylist> hes = [
  Happylist(
    name: 'somename',
    imageUrl: 'assets/images/someimage.jpg',
    url: 'someweblink',
  ),
  Happylist(
    name: 'somename',
    imageUrl: 'assets/images/someimage.jpg',
    url: 'someweblink',
  ),

And intialized with object named hes, so please help me to create a function in main.dart which generates the random element from that list. When it generates a random element how to access its url.

2 Answers 2

6

Import math library to generate random int;

import 'dart:math';

here is a pseudo list;

List yourList = ["first item", "second item", "third item"];

and then create a random in that will be used as index should be limited max number with the list's size;

int randomIndex = Random().nextInt(yourList.length);

and check the result;

print(yourList[randomIndex]);
Sign up to request clarification or add additional context in comments.

9 Comments

But my list conatins like this it contains two parameters both image url and url ,so please tell me how to select a random element from list and at the same time by tapping on it ,it should return its url.
@Rahul It would help if you provided an example of what your data looks like and what you expect. Given what information you provided in the question ("How to select random element from a list"), this answer would work.
this is my code class Happylist { String imageUrl; String url; String name; Happylist({ this.imageUrl, this.url, this.name, }); } List<Happylist> hes = [ Happylist( name: 'somename', imageUrl: 'assets/images/someimage.jpg', url: 'someweblink', ), so i want o select random element from that list .please help me
@Rahul 1. Put your code in your question, not in a comment. 2. This answer would let you pick a random a Happylist element from hes. If it's not working for you, then you need to say how it doesn't work.
|
0

Simply do this

List<Happylist> hes = List<Happylist>();

  hes.insert(
      0,
      new Happylist(
          name: 'somename',
          imageUrl: 'assets/images/someimage.jpg',
          url: 'someweblink'));
  hes.insert(
      1,
      new Happylist(
          name: 'somename1',
          imageUrl: 'assets/images/someimage1.jpg',
          url: 'someweblink'));
  hes.insert(
      2,
      new Happylist(
          name: 'somename2',
          imageUrl: 'assets/images/someimage2.jpg',
          url: 'someweblink'));

  var randomHappylist = (hes.toList()..shuffle()).first;
  print(randomHappylist.imageUrl);

check this also

9 Comments

means it return a element randomly can i get the url too.
this is my code class Happylist { String imageUrl; String url; String name; Happylist({ this.imageUrl, this.url, this.name, }); } List<Happylist> hes = [ Happylist( name: 'somename', imageUrl: 'assets/images/someimage.jpg', url: 'someweblink', ), so i want o select random element from that list .please help me
wait i will give answer
this is my problem i wnt to get the particular image along with on tap that particular url
hello it can get zeroth insdex
|

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.