Doubt Flutter: Listview duplicate data

Asked

Viewed 118 times

-1

Good morning!

Could someone assist me in a doubt?

  • What am I doing:

I made a search for the user to search for an Infraction in the app, and right after the action on the keyboard I redirect it to another page (Filtroinicioview) passing the data as parameter.

    onFieldSubmitted: (string) {
      print(filterNomeInfracao);
      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) => FiltroinicioView(filterNomeInfracao),
        ),
      );
    },

After redirecting the user to the View page of the searched infringement, I show the List of Infractions within one Listview.Builder, passing a Return in the list() and passing the filterNomeInfracao[index] (Here he will bring me all the data), and passing one String calling for 'INFRINGEMENTS'

  body: Column(
    children: <Widget>[
      Expanded(
        child: ListView.builder(
          padding: EdgeInsets.all(10.0),
          itemCount: filterNomeInfracao.length,
          itemBuilder: (BuildContext context, int index) {
            return Lista(
              filterNomeInfracao[index],
              'INFRAÇÕES'
            );
          },
        ),
      ),
    ],
  ),

And then inside Do Buildcontext (Top Code) I make the call to render the List that in the case would be a card, as it is below:

class Lista extends StatelessWidget {
  final Infracao infracao;
  final txtInfracoes;

  Lista(this.infracao, this.txtInfracoes);

  @override
  Widget build(BuildContext context) {
    return Column(
  children:  <Widget>[
    Card(child: ListTile(title: Text(txtInfracoes))),
    Card(
      child: ListTile(
        title: Text(infracao.descricao),
      ),
    ),
  ],
);
}
}

And in that he lists the infractions according to the print image, However, as you can see String "INFRINGEMENTS" is repeating, that is I needed it to be a fixed field, for example:

INFRAÇÕES
E aqui todas descrições

inserir a descrição da imagem aqui

  • THE EXPECTED RESULT WOULD THEN BE WITH STRING INFRINGEMENTS AS FIXED:

Print(done in Paint) below expected result:

inserir a descrição da imagem aqui

  • Is not repeating. Infringement and Attachments are being listed (manually)

  • I needed Infringement and Attachments to be fixed

  • Shows your entire class... How you are loading the data into filterNomeInfracao? Think if you were helping someone, I could tell you what the problem is without seeing the whole context? Edita your question and more information

  • 1

    Good morning, Matheus.

  • Dude, you did a good editing. But you see I also asked you the following "How you are loading the data into filterNomeInfracao?", your code displayed apparently is all OK, the problem is probably where you are taking the data from your API and clicking on the variable filterNomeInfracao. So of two one, either you are bringing repeated from your API or you are not loading right into the filter variable.

  • So it doesn’t involve the API if you notice I’m calling infraction.Description (this yes comes from the API) plus the string as the object to notice up there that I pass a Text string(Infractions) and call it in the list is the STRING that repeats and not my API data

Show 1 more comment

1 answer

1


You are setting the text "INFRACTIONS" fixed to each item in the list... Do the following:

  body: Column(
    children: <Widget>[
      Container(
        height: 30,
        width: double.infinity,
        child: Text("Infrações")
      ),
      Expanded(
        child: ListView.builder(
          padding: EdgeInsets.all(10.0),
          itemCount: filterNomeInfracao.length,
          itemBuilder: (BuildContext context, int index) {
            return Lista(
              filterNomeInfracao[index]
            );
          },
        ),
      ),
    ],
  ),

class Lista extends StatelessWidget {
  final Infracao infracao;

  Lista(this.infracao);

  @override
  Widget build(BuildContext context) {
    return Column(
  children:  <Widget>[
    Card(
      child: ListTile(
        title: Text(infracao.descricao),
      ),
    ),
  ],
);
}
}

That way it will work properly.

Now if you want the grid items grouped by "titles", that’s another story that escapes your question... But you can use Packge grouped_list

Browser other questions tagged

You are not signed in. Login or sign up in order to post.