Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Incorrect use of ParentDataWidget error when I use ListView

In this part of my application I have ListView, when I run app I get this error and I can't resolve that:

Scaffold(
  body: Directionality(
textDirection: TextDirection.rtl,
child: Container(
  child: StreamBuilder(
    stream: globals.database.ticketsDao.getTicketsStream(),
    builder: (BuildContext context, AsyncSnapshot<List<Tickets>> snapshot) {
      if (snapshot.hasData) {
        return Column(
          children: <Widget>[
            Expanded(
              child: ListView.separated(
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Flexible(
                        child: Text(
                          snapshot.data[index].subject,
                          style: Theme.of(context).textTheme.caption.copyWith(fontFamily: 'IranSansBold'),
                        ),
                      ),
                      subtitle: Text(
                        snapshot.data[index].description,
                        style: Theme.of(context).textTheme.caption.copyWith(fontFamily: 'IranSansLight'),
                      ),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider();
                  },
                  itemCount:  snapshot.data.length),
            ),
          ],
        );
      } else {
        return Container(
            child: Center(
          child: Text(
            Strings.notAnyTickets,),
          ),
        ));
      }
    },
  ),
),
));

error:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════  
The following assertion was thrown building DefaultTextStyle(debugLabel:
((englishLike subhead  2014).merge(blackMountainView

subhead)).copyWith, inherit: false, color: Color(0xdd000000), family:
Roboto, size: 16.0, weight: 400, baseline: alphabetic, decoration:
TextDecoration.none, softWrap:  wrapping at box width, overflow:
clip):  Incorrect use of ParentDataWidget.

Flexible widgets must be
placed directly inside Flex widgets.  Flexible(no depth, flex: 1,
dirty) has a Flex ancestor, but there are other widgets between them:
 - _ListTile

 - Padding(padding: EdgeInsets(16.0, 0.0, 16.0, 0.0))
 - Semantics(container: false, properties: SemanticsProperties, selected: false, label: null, value:  null, hint: null, hintOverrides:
null)

 - Listener(listeners: [down], behavior: opaque)
 - _GestureSemantics
 - Listener(listeners: <none behavior: translucent)
 - RepaintBoundary
 - IndexedSemantics(index: 0)
 - KeepAlive(keepAlive: false)
 - SliverList(delegate: SliverChildBuilderDelegate#6802e(estimated child count: 19))

 - SliverPadding(padding: EdgeInsets(0.0, 0.0, 0.0, 50.0))
 - Viewport(axisDirection: down, anchor: 0.0, offset: ScrollPositionWithSingleContext#c95ba(offset:
 0.0, range: null..null, viewport: 335.0, ScrollableState, AlwaysScrollableScrollPhysics - ClampingScrollPhysics,
IdleScrollActivity#05a39, ScrollDirection.idle))
 - IgnorePointer-[GlobalKey#a3d1a](ignoring: false, ignoringSemantics: false)

 - Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,  hintOverrides: null)
 - Listener(listeners: [down], behavior: opaque)
 - _GestureSemantics

 - Listener(listeners: [signal], behavior: deferToChild)
 - _ScrollSemantics-[GlobalKey#7cd1b]
 - RepaintBoundary
 - CustomPaint
 - RepaintBoundary
 - Expanded(flex: 1)  These widgets cannot come between a Flexible and its Flex.  The ownership chain for the parent of the offending
Flexible was:    DefaultTextStyle ← AnimatedDefaultTextStyle ←
_ListTile ← MediaQuery ← Padding ← SafeArea ←  Semantics ← Listener ← _GestureSemantics ← RawGestureDetector ← ⋯
like image 827
DolDurma Avatar asked Jul 01 '19 15:07

DolDurma


People also ask

What is incorrect use of ParentDataWidget?

This error occurs when the Child widget has not matched the parent widget.

What is ParentDataWidget?

ParentData, the superclass of the data that will be placed in RenderObject. parentData slots. The T type parameter for ParentDataWidget is a ParentData. RenderObjectWidget, the class for widgets that wrap RenderObjects. StatefulWidget and State, for widgets that can build differently several times over their lifetime.

What is flutter flexibility?

Flexible is a built-in widget in flutter which controls how a child of base flex widgets that are Row, Column, and Flex will fill the space available to it. The Expanded widget in flutter is shorthand of Flexible with the default fit of FlexFit. tight.


2 Answers

Gives this error because A Flexible widget must be a descendant of a Row, Column, or Flex,

Using a Flexible widget gives a child of a Row, Column, or Flex the flexibility to expand to fill the available space in the main axis (e.g., horizontally for a Row or vertically for a Column)

fixing the above issue there is two way,

1: is to move the Flexible widget inside a flex widget. Example below

ListView.separated(
   itemBuilder: (context, index) {
   return Container(
       height: 50,
       child: Column(
         mainAxisAlignment: MainAxisAlignment.end,
         children: <Widget>[
           Flexible(child: Text('Title')),
           Flexible(child: Text("Sub Title"))
         ],
       ),
     );
   },
separatorBuilder: (context, index) {
return Divider();
},
itemCount:  snapshot.data.length)

2:Remove the Flexible widget

ListTile(
 title: Text(
 snapshot.data[index].firstAndLastName,
 style: Theme.of(context).textTheme.caption.copyWith(fontFamily: 'IranSansBold'),
),
subtitle: Text(
snapshot.data[index].firstAndLastName,
style: Theme.of(context).textTheme.caption.copyWith(fontFamily: 
'IranSansLight'),
  ),
);
like image 185
Paresh Mangukiya Avatar answered Nov 01 '22 03:11

Paresh Mangukiya


Flexible Widgets must be the direct child of an Expanded or Flex Widget.

In your case, you are using Flexible as a part of a ListTile widget, which is not a suitable parent.

Flexible widgets must be placed directly inside Flex widgets.

Flexible has a Flex ancestor, but there are other widgets between them: - _ListTile

like image 36
omar hatem Avatar answered Nov 01 '22 04:11

omar hatem