Generate Random Widget List in Flutter

I'll tell you how you can generate a list with random positions of Widgets in Flutter.

I'm going to show you how you can generate a random list of elements, that is, elements that can be integers, that is, floating point numbers, guanos or objects as is my case, so the scheme that I am following here is really very simple, look at the construction part of the list, the important part is that it would be through an API, but obviously you can have a list somewhere, static or generated in another way, here we finally have the list at the time of construction, go here I added what is an additional parameter:

class QuestionModel {
  int id;
  String title;
  String tagTitle;
  String tagDescription;
  String type;
  int tagId;
  int questionsQuantity;
  String language;
  int randomOrder = 0;
  List<QuestionSimpleModel> questionsSimple = [];

  QuestionModel.fromJson(Map<String, dynamic> qMap)
      : id = qMap['id'],
        title = qMap['title'],
        tagTitle = qMap['tag_title'],
        tagDescription = qMap['tag_description'],
        tagId = qMap['tag_id'],
        type = qMap['type'],
        questionsQuantity = qMap['questions_quantity'] ?? 0,
        randomOrder = Random().nextInt(100), // orden aleatorio
        language = qMap['language'];
}

That is to indicate the order this can be a little inefficient this is something that I am evaluating how I can correct it a little since we are creating if we have for example a list of 1000 elements then here we would be creating 1000 instances of Random which would be inefficient since with a single instance we could generate the random number therefore here it could be passed as a parameter and I build it where this method is being invoked it could be a solution but at the moment I have it like this for my case it is simply 15 questions so 15 questions at a time so it does not affect me much but I leave it there pending in summary here you place in this case list of objects the Data that you want to handle in my case is what you are seeing on the screen and additionally you create a parameter that is to generate the random order here feel free to as who says modify it as you want perfectly you can also create a base class that has this parameter to indicate that it is going to be ordered and receive
a generic instance a dynamic one that is the model here so in case you do not want between quotes dirty the distance of the class or the definition of the class with a parameter of this type that has absolutely nothing to do with the class you can create a class in a wrapper way So to speak, that is to say a class called order and there you define this parameter and then the parameter that is of type Dynamic or a template and here you define what is the model itself that you are working on but anyway this is a simple way to do it without many problems this is simply to say with this it is not being sorted randomly where we do it is precisely here here we are as who says building our list again here this can be anything in my case it is a request to my apir and here through the method called sort:

_questions = await QuestionHelper.getQuestionGetByPrincipalId(
       widget.id, userPreference.locale);
   // orden aleatorio
   _questions.sort((a, b) => a.randomOrder.compareTo(b.randomOrder));

What we do is the sorting the sorting based on some parameter in this case it is by the randomly generated number and it is that simple for the rest there is nothing else to do in practice we have the following I am going to come here to the question module we come to question I am going to grab one for example javascript basics here we have it as is you can see this It would be for the first part This is the first thing it is asking what methods it uses to write to the browser console now it would be What is the return value let's enter the module again now notice that it is asking something else therefore the random sorting that we did before is working correctly.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 2d 10:22!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

View books
¡Become an affiliate on Gumroad!