0

item1, item2, item3 are all lists and i am trying to build a list view with all the items that each list holds, where all this three listview builders would take as much place as they need, lets say that item1 has 20 items in it and it will take 20 rows, and item2 has 25 etc. When i try to use a row and listview.builder it gives me an error. What I am trying to do:

body: Container(
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    return ListTile(
                      title: Text(widget.item1[index]),
                    );
                  },
                  itemCount: widget.item1 == null ? 0 : widget.item1.length,
                ),
              ],
            ),
          ],
        ),
      ),

Among a huge list of crash report:

flutter: Another exception was thrown: NoSuchMethodError: The method '<=' was called on null.
flutter: Another exception was thrown: NoSuchMethodError: The getter 'visible' was called on null.

The problem is that the only way I know is to make it with an Expanded, and it will divide the screen in three and make equal space or i can manipulate with flex, but this is not what i want.

body: Container(
        child: Column(
          children: <Widget>[
            Expanded(
              child: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(widget.item1[index]),
                  );
                },
                itemCount: widget.item1 == null ? 0 : widget.item1.length,
              ),
            ),
            Expanded(
              child: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(widget.item2[index]),
                  );
                },
                itemCount: widget.item2 == null ? 0 : widget.item2.length,
              ),
            ),
            Expanded(
              child: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(widget.item3[index]),
                  );
                },
                itemCount: widget.item3 == null ? 0 : widget.item3.length,
              ),
            ),
          ],
        ),
      ),
8
  • why don't you use SingleChildScrollView + Column , and add your items programmatically before add as a children of your Column Commented Jun 12, 2019 at 1:37
  • @diegoveloper I was not aware of this, I am looking at it now. Commented Jun 12, 2019 at 1:40
  • you can use Slivers also, I have a question , item1,item2,item3 have the same type of objects? Commented Jun 12, 2019 at 1:41
  • Yes item1, item2, item3, are all list objects. Slivers are a little bit more advanced i suppose. But i can try slivers too. Commented Jun 12, 2019 at 1:42
  • yes I know, but item1, item2 , item3 have the same type of object? List<Foo> item1 , List<Foo> item2 , List<Foo> item3 ? if it's true you can combine your lists Commented Jun 12, 2019 at 1:43

1 Answer 1

2

This is what I was talking about, a List using SingleChildScrollView and Column, you can also do the same with Slivers

A sample I made for you:

 final List<String> item1 = List.generate(5, (val) => "item1 $val");
  final List<String> item2 = List.generate(5, (val) => "item2 $val");
  final List<String> item3 = List.generate(5, (val) => "item3 $val");

  @override
  Widget build(BuildContext context) {
    final items = <Widget>[];

    for (String i1 in item1) {
      items.add(ListTile(
        title: Text(i1),
      ));
    }

    for (String i2 in item2) {
      items.add(ListTile(
        title: Text(
          i2,
          style: TextStyle(
            color: Colors.red,
          ),
        ),
      ));
    }

    for (String i3 in item3) {
      items.add(ListTile(
        title: Text(
          i3,
          style: TextStyle(
            color: Colors.blue,
          ),
        ),
      ));
    }

    return Scaffold(
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            children: items,
          ),
        ),
      ),
    );
  }
}

Another way using ListView.builder :

 return Scaffold(
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            children: [
              ListView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(item1[index]),
                  );
                },
                itemCount: item1.length,
              ),
              ListView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(
                      item2[index],
                      style: TextStyle(
                        color: Colors.red,
                      ),
                    ),
                  );
                },
                itemCount: item2.length,
              ),
              ListView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(item3[index]),
                  );
                },
                itemCount: item3.length,
              ),
            ],
          ),
        ),
      ),
    );
  }

Don't forget to check this awesome article about Slivers by Emily Fortuna (Flutter team)

https://medium.com/flutter/slivers-demystified-6ff68ab0296f

Sign up to request clarification or add additional context in comments.

2 Comments

I added another way using ListView.builder :) , It's better in terms of performance.
Thats even better, thank you very much, i really did learn something, cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.