Home > database >  "Incorrect use of ParentDataWidget" when using a Gridview in Flutter
"Incorrect use of ParentDataWidget" when using a Gridview in Flutter

Time:12-08

Iam having trouble getting a flexible heigth for my Gridview and are getting a bit lost here. This is my error and below is my code. Any ideas please?

"The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.

Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a RepaintBoundary widget."

@override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: SafeArea(
          child: Column(children: <Widget>[
            AppBar(
              backgroundColor: Colors.grey,
              actions: [
                IconButton(onPressed: () {}, icon: Icon(Icons.search)),
              ],
            ),
        GridView.count(
              primary: false,
              crossAxisCount: 4,
              shrinkWrap: true,
              //scrollDirection: Axis.vertical,
              //childAspectRatio: MediaQuery.of(context).size.width /
              //   (MediaQuery.of(context).size.height / 4),
              children:
                  List.of(foodBrain.getFoodList().map((food) => ImageCircle(
                        fileName: food.fileName,
                        displayName: food.displayName,
                        text: food.text,
                      ))),
            ),
        Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.only(top: 5.0, right: 16.0),
                  child: Text(
                    'Click when done',
                    style: kButtonTextStyle,
                  ),
                ),
                IconButton(
                  onPressed: () {
                    //Here we just pass to the passover screen, in main we set the argument to pass.
                    Navigator.pushNamed(context, PassoverList.id);
                  },
                  icon: Icon(FontAwesomeIcons.list),
                  iconSize: 30.0,
                  color: Colors.blueAccent,
                ),
              ],
            ),


  

CodePudding user response:

Wrap GridView.count with Expanded widget.

        Expanded(
              child: GridView.count(
                primary: false, crossAxisCount: 4, shrinkWrap: true,
                //scrollDirection: Axis.vertical,
                //childAspectRatio: MediaQuery.of(context).size.width /
                //   (MediaQuery.of(context).size.height / 4),
                children: [
                  ...List.generate(44, (index) => Text(index.toString()))
                ],
              ),
            ),

CodePudding user response:

I think I just solved it by following the directions/explanation in the console. Big surprise :) I just let the Expanded as the parent of Gridview be (as Yeasin had as suggestion above) but removed the one that was in the ImageView class. I just followed "the ownership chain for the renderObject" like it said.

======== Exception caught by widgets library ======================================================= The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.

Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a RepaintBoundary widget.

The ownership chain for the RenderObject that received the incompatible parent data was: Padding ← Container ← Expanded ← ImageCircle ← RepaintBoundary ← IndexedSemantics ← NotificationListener ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← ⋯

  • Related