Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

flutter : use index in position , stack parent

i have to use index in position , but when I use listView.builder for I , I got this error :

Incorrect use of ParentDataWidget.

The ParentDataWidget Positioned(left: 157.2) wants to apply ParentData
of type StackParentData to a RenderObject, which has been set up to
accept ParentData of incompatible type FlexParentData.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Usually, this means that the Positioned widget has the wrong ancestor
RenderObjectWidget. Typically, Positioned widgets are placed directly
inside Stack widgets. The offending Positioned is currently placed
inside a Row widget.

and app crashed.

i use stack -> listview.bulder(for index) -> position

but correct is stack -> position

I have to use index in position.
so can anyone help me how can I do that ?

for example myList are :

myList = [{"data" : 100.0},{"data" : 200.0 },{"data" : 300.0 }, {"data" :400.0 },{"data" : 500.0} ]

here is my code :

Stack(
                  alignment: AlignmentDirectional.topStart,
                  children: [
                    Positioned(
                          left: (myList[index]["data"]),
                          child: Directionality(
                            textDirection: TextDirection.ltr,
                            child: MySimpleIcons(
                              iconName: MyIcons.bookProfile,
                              iconColor: theme.darkGrey,
                              iconSize: 24,
                            ),
                          ),
                 // another widgets
                 ) ],

>Solution :

You can use .map & spread operator to fulfill this case. You can try the following code:

Stack(
      alignment: AlignmentDirectional.topStart,
      children: [
        ...myList.map((position) => Positioned(
                  left: position,
                  child: Directionality(
                    textDirection: TextDirection.ltr,
                    child: MySimpleIcons(
                      iconName: MyIcons.bookProfile,
                      iconColor: theme.darkGrey,
                      iconSize: 24,
                    ),
                  ),
                )
            // another widgets
            )
      ],
    );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading