Flutter Comment afficher la liste par boucle for

Dec 01 2020

J'ai créé une liste simple pour l'afficher par boucle for.

List categories = [
  {
    {
      'CatID': '0',
      'CatName': 'All'
    },
    {
      'CatID': '1',
      'CatName': 'Computer Hardware'
    },
    {
      'CatID': '2',
      'CatName': 'Computer Software'
    },
  }
];

alors je définis un widget comme celui-ci

List<Widget> CatWidget = List<Widget>(); 

Alors je l'utilise comme ça

for (int i = 0; i < 8; i++) {
  CatWidget.add(
    Container(
      child: Text(categories[i]['CatName']),
    ),
  );
}

Son erreur d'affichage Class '_CompactLinkedHashSet<Map<String, String>>' has no instance method '[]'.

Réponses

3 nvoigt Dec 01 2020 at 15:51

Votre liste contient trop d'accolades:

List categories = [
    {
      'CatID': '0',
      'CatName': 'All'
    },
    {
      'CatID': '1',
      'CatName': 'Computer Hardware'
    },
    {
      'CatID': '2',
      'CatName': 'Computer Software'
    },
  ];

Et votre boucle devrait fonctionner à 3 pas 8 parce que vous n'avez 8.

Donc, ce qui suit fonctionnerait, cependant, je vous encourage à vérifier l'autre réponse pour une meilleure approche en général:

import 'package:flutter/material.dart';

void main() {

    List categories = [
  
    {
      'CatID': '0',
      'CatName': 'All'
    },
    {
      'CatID': '1',
      'CatName': 'Computer Hardware'
    },
    {
      'CatID': '2',
      'CatName': 'Computer Software'
    },
  ];

final catWidget = List<Widget>(); 

for (int i = 0; i < 3; i++) {
  catWidget.add(
    Container(
      child: Text(categories[i]['CatName']),
    ),
  );
}

  runApp(MyApp(catWidget));
}

class MyApp extends StatelessWidget {
  final List<Widget> children;
  
  const MyApp(this.children);
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(children: children),
        ),
     );
  }
}
3 UsmanAkhlaq Dec 01 2020 at 15:43

Vous pouvez utiliser list.generate pour parcourir votre liste et renvoyer un widget.

 Column(
            children: List.generate(
              tags.length,
              (index) => Container(
                // margin: EdgeInsets.symmetric(),
                child: NativeButton(
                  borderRadius: BorderRadius.circular(4),
                  padding: EdgeInsets.symmetric(
                    vertical: sizeConfig.height(.01),
                    horizontal: sizeConfig.width(.02),
                  ),
                  color: Colors.grey[200],
                  child: Text(tags[index]),
                ),
              ),
            ),
          ),
RohitSingh Dec 01 2020 at 16:03

Vous avez un tableau JSON, à l'intérieur du tableau JSON, vous avez un objet JSON et à l'intérieur de cet objet JSON, vous avez plusieurs objets JSON, que vous souhaitez itérer un par un, ce qui est inapproprié.

Tableau JSON> Objets JSON True

Tableau JSON> Tableau JSON> Objet (s) JSON True

Tableau JSON> Objet JSON> Objet (s) JSON False

La boucle For ne peut fonctionner que sur un tableau ou une liste d'objets et le json devrait être comme:

  [
    {
      'CatID': '0',
      'CatName': 'All'
    },
    {
      'CatID': '1',
      'CatName': 'Computer Hardware'
    },
    {
      'CatID': '2',
      'CatName': 'Computer Software'
    },
  ]