0

I am using GetX. I need to display list from JSON.

class JobModelListView extends StatelessWidget {
  final modelController = Get.put(JobModelList());
  @override 
  build(BuildContext context) {
    return Obx(() =>
     ListView.builder(   
      itemBuilder: (ctx, idx) => JobModelView(modelController.jobList[idx], key: UniqueKey()),
      itemCount: modelController.jobList.length
    ));
   }
}

Single Element Item:

class JobModelView extends StatelessWidget {
  final JobModel jobModel;
  JobModelView(this.jobModel, {Key? key}) : super(key: key);

  final modelController = Get.put(JobModelList());
 
  @override 
  build(BuildContext context) { 
  return Row(
           children: [
             Container(
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              child: 
                CustomPaint(
                  size: Size(50, 100),
                  painter: JobModelPainter(jobModel.parserStatus),
                ),
              
            ),
             
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                  Text("""PID: ${jobModel.jobPid}"""), 
                  Text("""Queue: ${jobModel.unprocessedFileCount}"""), 
                  Text("""Jobs: ${jobModel.jobNumbers}"""), 
                  Text("""Section: ${jobModel.currentSection}"""), 
                  Text("""LastPingDate: ${jobModel.lastPingDate == null ? '' : timeago.format(jobModel.lastPingDate!, locale: 'en_short')} ago"""),
                  Text("""ParserStatus: ${jobModel.parserStatus.toString().split(".").last}"""),
                  Text("""isSomeUnknownTags: ${jobModel.isSomeUnknownTags}"""),
                  Text("""Region: ${jobModel.region}"""),
                  Text("""Msg: ${jobModel.msg}"""),

                ])),
            
            SizedBox(
              height: 45, 
              child: ElevatedButton(
                 style: ElevatedButton.styleFrom(
                  primary: Colors.red, // background
                  onPrimary: Colors.white, // foreground
                ),
               child: Text("KILL"),
             onPressed: () async {
               await modelController.removeByPid(jobModel.jobPid!);
               await modelController.fetchModelList();
             },
             ))
  
         ],
         
        );
  }
}

I need to save order of elements on every redraw (getting updated dates from JSON). But they are randomly change order. Sometimes pid: 4712 on top, sometimes another pid.

enter image description here

Hot to fix it?

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

0 Answers0