0

Guys I have a question for you: I have built a PageView. In this PageView is a ListView and in this ListView is a list of widgets. But unfortunately my code doesn't work for some reason ... And I have no idea why. I'm still very new to Flutter. I would really appreciate your help. The code that generates the error is the following:

Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView(
        children: createHomePage(
            controller: _controller,
            content: widget.content,
            title: widget.title,
            menuButtonAction: widget.menuButton,
            showShadow: widget.showShadow,
            context: context),
      ),
    );
  }
}

I would be really grateful if you could help me. The code works fine when I replace the ListView with a Colum, but then unfortunately I can't scroll ... But it has to be scrollable! I keep getting the following error:

I/flutter ( 4539): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════

I/flutter ( 4539): The following assertion was thrown building NotificationListener<KeepAliveNotification>:

I/flutter ( 4539): Incorrect use of ParentDataWidget.

I/flutter ( 4539): Expanded widgets must be placed inside Flex widgets.

I/flutter ( 4539): Expanded(no depth, flex: 1, dirty) has no Flex ancestor at all.

I/flutter ( 4539): The ownership chain for the parent of the offending Expanded was:

I/flutter ( 4539):   RepaintBoundary ← IndexedSemantics ← 
NotificationListener<KeepAliveNotification> ← KeepAlive ←

I/flutter ( 4539): AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ← ShrinkWrappingViewport

I/flutter ( 4539): ← ⋯

I/flutter ( 4539): 

I/flutter ( 4539): The relevant error-causing widget was:

I/flutter ( 4539):   ListView

How can I solve this problem? I've tried a lot. For example, I put the list of widgets in a column and changed the ListView to a SingleChildListView, but that didn't help either. However, I got another error message. This was:

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════

I/flutter ( 4539): The following assertion was thrown during performLayout():

I/flutter ( 4539): RenderFlex children have non-zero flex but incoming height constraints are unbounded.

I/flutter ( 4539): When a column is in a parent that does not provide a finite height constraint, for example if it is

I/flutter ( 4539): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a

I/flutter ( 4539): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining

I/flutter ( 4539): space in the vertical direction.

I/flutter ( 4539): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child

I/flutter ( 4539): cannot simultaneously expand to fit its parent.
I/flutter ( 4539): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible

I/flutter ( 4539): children (using Flexible rather than Expanded). This will allow the flexible children to size

I/flutter ( 4539): themselves to less than the infinite remaining space they would otherwise be forced to take, and

I/flutter ( 4539): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum

I/flutter ( 4539): constraints provided by the parent.

I/flutter ( 4539): If this message did not help you determine the problem, consider using debugDumpRenderTree():

I/flutter ( 4539):   https://flutter.dev/debugging/#rendering-layer

I/flutter ( 4539):   http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html

I/flutter ( 4539): The affected RenderFlex is:

I/flutter ( 4539):   RenderFlex#bfee3 relayoutBoundary=up15 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(creator: Column ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ← ShrinkWrappingViewport ← IgnorePointer-[GlobalKey#665ae] ← ⋯, parentData: <none> (can use size), constraints: BoxConstraints(w=411.4, 0.0<=h<=Infinity), size: MISSING, direction: vertical, mainAxisAlignment: start, mainAxisSize: max, crossAxisAlignment: center, verticalDirection: down)

I/flutter ( 4539): The creator information is set to:

I/flutter ( 4539):   Column ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ←

I/flutter ( 4539):   KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← MediaQuery ← SliverPadding ←

I/flutter ( 4539):   ShrinkWrappingViewport ← IgnorePointer-[GlobalKey#665ae] ← ⋯

I/flutter ( 4539): See also: https://flutter.dev/layout/

I/flutter ( 4539): If none of the above helps enough to fix this problem, please don't hesitate to file a bug:

I/flutter ( 4539):   https://github.com/flutter/flutter/issues/new?template=BUG.md

I/flutter ( 4539): 

I/flutter ( 4539): The relevant error-causing widget was:

I/flutter ( 4539):   Column

In the second example, the screen turns white, but nothing happens and nothing works. At the first error the screen turns red and I get the error that you can see in the picture!

Red Error on the Screen

Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
Lars Henning
  • 47
  • 1
  • 7
  • createHomePage ad this. Try to run by wrapping listview with expanded and wrap expanded with Column widget. – Viren V Varasadiya Apr 03 '20 at 14:09
  • First of all, thank you for your help and your answer. I'm really glad that someone came forward. But unfortunately that doesn't work. I don't know why, but I still get the same error. If I replace the ListView with a column, everything works fine. Neither the container, nor the expanded or the column help. Also shrikWrap doesn't help.... Do you have another idea? – Lars Henning Apr 04 '20 at 08:51
  • add code of createHomePage and i suggested you to wrap not replace. – Viren V Varasadiya Apr 04 '20 at 08:59
  • Thank you for your answer. And your efforts to help me. I once filmed a short video for you from my PC to show you the problem. You can find the video at: drive.google.com/file/d/1MkP-BIJba9F8WB01SIwvvBByYRwY4zHr/… I don't know exactly where the problem is, but it just doesn't work. You will see in the video that I tried everything you said. And I have a rough plan of what I'm doing. I just can't explain why it doesn't work ... – Lars Henning Apr 04 '20 at 09:42
  • I really hope that one of you can help me. Maybe I'm just stupid and don't see the mistake ... But I've been working on this problem for days and can't find a solution – – Lars Henning Apr 04 '20 at 09:42
  • can you add createHomePage page code? something is wrong over there. – Viren V Varasadiya Apr 04 '20 at 09:45
  • of course! Thank you for your help. I am really happy to find support here. Unfortunately, the code is too long to post here. So I copied it into a Word document for you. You can find this at: https://drive.google.com/file/d/1MbJnzc3p2vXNWOM-a6vtZUGw5k6DwrQf/view?usp=sharing – Lars Henning Apr 04 '20 at 10:15
  • That is the complete code of the HomePage. I also added the list of widgets to the ListView. – Lars Henning Apr 04 '20 at 10:20
  • i don’t know what is your desire output screen but if you remove expanded widget from listview then it will work. – Viren V Varasadiya Apr 04 '20 at 10:43
  • You are the best! Thank you for your help! That solved my problem. Thank you! – Lars Henning Apr 04 '20 at 10:45
  • Welcome. I am really glad to hear that it helps you. Accept my answer, so other also get benefit. @lars henning – Viren V Varasadiya Apr 04 '20 at 11:00

2 Answers2

1

Using Of Expanded widget in listview is not valid because expanded widget doesn’t get any height to expand, so removing expanded widget solve your issue.

Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
0
I/flutter ( 4539): RenderFlex children have non-zero flex but incoming height constraints are unbounded.

I/flutter ( 4539): When a column is in a parent that does not provide a finite height constraint, for example if it is

I cant' see the complete code for now, but what this means for you is, that your listview needs a kind of height which is give for example if you wrap it inside a container.

Second thing you can do is put shrinkwrap: true in listview

import 'package:flutter/material.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Center(
          child: Container(
            margin: EdgeInsets.all(32),
            decoration: BoxDecoration(border: Border.all(color: Colors.red)),
            child: ListView(
              shrinkWrap: true,
              children: <Widget>[
                ListTile(title: Text('Item 1')),
                ListTile(title: Text('Item 2')),
                ListTile(title: Text('Item 3')),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

your listview then only takes the height of all your widgets or items inside the listview itself.

Hope it helps.

Marcel Dz
  • 2,321
  • 4
  • 14
  • 49
  • Thanks for your help, but unfortunately, as you can see in the video that I shot for viruses, that doesn't help. And I really don't know why ... hopefully you can think of something ... – Lars Henning Apr 04 '20 at 09:45