Flutter: Error when adding more than 3 items to a Listview

Asked

Viewed 601 times

-1

Hello, all the Listview of my application in Flutter if add more than 3 items occurs to Exception below:

I/flutter (13259): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (13259): The following assertion was thrown building I/flutter (13259): Rawgesturedetector-[Labeledglobalkey#fd1f4](state: I/flutter (13259): Rawgesturedetectorstate#a263f(Estures: , behavior: Paque)): I/flutter (13259): 'package:flutter/src/Rendering/sliver_multi_box_adaptor.Dart': Failed assertion: line 263 pos 16: I/flutter (13259): 'Child == null || indexof(Child) > index': is not true.

After this Exception, I try to go to another screen and the following error appears in my Listview, then expands to the whole screen!

I/flutter ( 7826): Another Exception was thrown: 'package:flutter/src/widgets/framework.Dart': Failed assertion: line 1772 pos 12: '_Elements.contains(element)': is not true.

exception

Someone’s been through this and can help me?

Widget Code:

 @override
  void dispose() {
    listViewController.removeListener(_scrollListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: true,
      key: _scaffoldstate,
     body: (_listaTela.length == 0)
      ? new AppLayout(
        navBar: NavBar(
          title: "$_titulo",
        ),
        widget: Column(
          //crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(
                top: 15.0,
              ),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(left: 35.0, right: 15.0),
                    child: Container(
                        width: 45.0,
                        height: 45.0,
                        decoration: BoxDecoration(
                            border: Border.all(width: 1.0, color: Colors.grey),
                            shape: BoxShape.circle),
                        child: new Icon(Icons.shopping_basket)),
                  ),
                  Text("$_titulo",
                      style: TextStyle(
                          fontSize: 16.0, fontWeight: FontWeight.w600))
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(
                  left: 25.0, right: 25.0, top: 10.0, bottom: 15.0),
              child: Divider(
                height: 1.0,
              ),
            ),
            // sem itens
            Offstage(
              offstage: !_semItens,
              child: Column(
                children: <Widget>[
                  Icon(
                    Icons.list,
                    size: 50.0,
                  ),
                  Text('Sem itens'),
                  FlatButton(
                    onPressed: () {
                      _resetLista();
                    },
                    child: Text('Atualizar'),
                  )
                ],
              ),
            ),
            // loading
            Offstage(
              offstage: !_ocupado,
              child: Center(child: CircularProgressIndicator()),
            ),
                  ],
                ),
              ) 
              : new AppLayout(
        navBar: NavBar(
          title: "$_titulo",
        ),
        widget: Column(
          //crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(
                top: 15.0,
              ),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(left: 35.0, right: 15.0),
                    child: Container(
                        width: 45.0,
                        height: 45.0,
                        decoration: BoxDecoration(
                            border: Border.all(width: 1.0, color: Colors.grey),
                            shape: BoxShape.circle),
                        child: new Icon(Icons.shopping_basket)),
                  ),
                  Text("$_titulo",
                      style: TextStyle(
                          fontSize: 16.0, fontWeight: FontWeight.w600))
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(
                  left: 25.0, right: 25.0, top: 10.0, bottom: 15.0),
              child: Divider(
                height: 1.0,
              ),
            ),
            // sem itens
            Offstage(
              offstage: !_semItens,
              child: Column(
                children: <Widget>[
                  Icon(
                    Icons.list,
                    size: 50.0,
                  ),
                  Text('Sem itens'),
                  FlatButton(
                    onPressed: () {
                      _resetLista();
                    },
                    child: Text('Atualizar'),
                  )
                ],
              ),
            ),
            // loading
            Offstage(

 Flexible(
              child: ClipRect(
                child: RefreshIndicator(
                  onRefresh: _resetLista,

                  child: ListView(
                    padding: const EdgeInsets.only(
                      left: 4.0,
                      right: 4.0,
                    ),
                      controller: listViewController,
                      children: _listaTela.map((it) {
                        return Dismissible(
                          background: Container(color: Colors.red,),
                          key: ValueKey("remover"),
                          onDismissed: (direction){
                            setState((){
                              _removerItem(it);
                            });
                          },
                          child: ListTile(
                          onTap: () {
                            _itemSel(it, _listaTela.indexOf(it));
                          },
                          title: Row(
                            children: <Widget>[

                              Padding(
                                  padding: EdgeInsets.only(right: 12.0),
                                  child: Container(
                                    height: 40.0,
                                    width: 50.0,
                                    child: Icon(
                                      Icons.label,
                                      color: Colors.white,
                                      size: 30.0,
                                    ),
                                    color: Colors.blueGrey,
                                  )),
                              Flexible(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Padding(
                                          padding: const EdgeInsets.only(
                                              bottom: 4.0),
                                          child: Text(
                                            it.produto.descricao.toString(),
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                                fontSize: 15.0,
                                                letterSpacing: 1.0,
                                                fontWeight: FontWeight.bold),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                        Padding(
                                          padding: const EdgeInsets.only(
                                              bottom: 4.0),
                                          child: Text(
                                            it.quantidade.toString(),
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: 15.0,
                                              letterSpacing: 1.0,
                                            ),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        )
                        );
                      }).toList()),
                ),
              ),
            ),

            Padding(
              padding: const EdgeInsets.only(bottom: 8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: Text(
                      '$_lblTot',
                      style: const TextStyle(color: Colors.grey),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: SpeedDial(
        animatedIcon: AnimatedIcons.menu_close,
        children: [
          SpeedDialChild(
            child: Icon(Icons.add),
            label: "Adicionar item",
            onTap: () {
              _add();
            }, 
            ),
            SpeedDialChild(
            child: Icon(Icons.send),
            label: "Enviar pedido",
            backgroundColor: Colors.green,
            onTap: () {
              _enviar();
            } 
            )
        ],
      ),
    );
  }
}
  • Show the code you made.

  • Without the code it’s not possible to analyze

  • I’ve added the Listview code, thanks in advance.

  • It would be interesting an example that it was possible to reproduce, take your code and execute.. If it is not possible to pass, I would recommend removing Widget by Widget from the tree to identify where the problem is.

  • @Juliohenriquebitencourt I will create a code that to reproduce, thank you.

  • @Denerportela show! It will facilitate the analysis.

  • @Juliohenriquebitencourt I put the project on Github: https://github.com/denerportela/flutter_listview_bug_itens 1. Download the project. 2. Run. 3. Click the Floating Menu Button 4. Click the + After the 3rd click the error will appear!

  • If the answer helped/answered your question, mark it as accepted.

Show 3 more comments

1 answer

0

I took a look at the project you put on Github. Your code is confused, disorganized and in some out-of-format locations.

When rotating already occurs the error:

E/flutter (21316): Receiver: null E/flutter (21316): Tried Calling: length E/flutter (21316): #0 Object.noSuchMethod (Dart:core-patch/object_patch.Dart:50:5) E/flutter (21316): #1
_Myhomepagestate. _getData. (package:test_listview/main.Dart:117:45)

Are you calling the lenght in a null object, you need to review its logic when using asynchronous programming.

In fact, after adding the items in your example another error occurs. But for sure if you review the logic of your screen, rebuild the example again step by step, and follow the principles of quality will be able to identify where you are missing.

The problem is not in ListView, if you debug will notice, it is in the Widgets structure that you are using.

Recommend:

  • Organize code, better separate methods/widgets responsibilities, rebuild your code from scratch.
  • Follow general good coding practices in Dart, variable naming, code formatting (use the IDE with the plugin for formatting), package file organization, etc.
  • Go adding step by step and check when exactly the error starts to occur.
  • Do not commit the .idea for the versioned repository, this is a local IDE configuration.
  • 1

    Thanks for the programming tips Julio, as I’m starting will help a lot not only in this project as in others. Really the main problem here is the organization and good practices. But that I will acquire with time or in a programming course. A time unfortunately I do not have now by the tight deadline. I will see what I can. Thank you very much anyway.

  • Of course, we’ve all been and are beginners all the time. I mentioned good practices and organization because they help us better understand what is happening in our code and find where the problems are, what is causing a certain error to occur. And of course, they make it easier when you’re working as a team and someone else also needs to understand your code. If you have any specific questions about some behavior please ask here :)

Browser other questions tagged

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