Flutter - Incorrect use of ParentDataWidget

Viewed 2682

Since I introduced a PageView widget, I get this error:

════════ 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 ← AnimatedContainer-[LabeledGlobalKey<ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget>>#758ef] ← KeyboardAvoider ← Expanded ← VpFormContainer ← LoggedOutNickNamePage ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← ⋯ When the exception was thrown, this was the stack
#0      RenderObjectElement._updateParentData.<anonymous closure> package:flutter/…/widgets/framework.dart:5770
#1      RenderObjectElement._updateParentData package:flutter/…/widgets/framework.dart:5786
#2      RenderObjectElement.attachRenderObject package:flutter/…/widgets/framework.dart:5808
#3      RenderObjectElement.mount package:flutter/…/widgets/framework.dart:5501
#4      SingleChildRenderObjectElement.mount package:flutter/…/widgets/framework.dart:6117 ...

════════ 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 ← AnimatedContainer-[LabeledGlobalKey<ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget>>#758ef] ← KeyboardAvoider ← Expanded ← VpFormContainer ← LoggedOutNickNamePage ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← ⋯ When the exception was thrown, this was the stack
#0      RenderObjectElement._updateParentData.<anonymous closure> package:flutter/…/widgets/framework.dart:5770
#1      RenderObjectElement._updateParentData package:flutter/…/widgets/framework.dart:5786
#2      RenderObjectElement.attachRenderObject package:flutter/…/widgets/framework.dart:5808
#3      RenderObjectElement.mount package:flutter/…/widgets/framework.dart:5501
#4      SingleChildRenderObjectElement.mount package:flutter/…/widgets/framework.dart:6117 ...

I have been trying fixes for the last couple of hours, but nothing is working. Does anyone know exactly where to apply a fix and what is the fix?

Here is the PageView:

class LoggedOutPageView extends StatelessWidget {
  final _controller = PageController(
    initialPage: 0,
  );

  @override
  Widget build(BuildContext context) {
    print('building loggedOutPageView');

    final pageView = PageView(
      physics: const NeverScrollableScrollPhysics(),
      controller: _controller,
      clipBehavior: Clip.none,
      children: [
        LoggedOutNickNamePage(_controller),
        LoggedOutEmailPage(_controller),
        LoggedOutPasswordPage(),
      ],
    );

    final _pageContent = Expanded(
        child: Container(child: pageView, color: Colors.transparent), flex: 1);

    final _pageIndicator = Container(
        height: 50,
        child: SmoothPageIndicator(
          controller: _controller,
          count: 3,
          effect: const JumpingDotEffect(),
        ),
        color: Colors.transparent);

    return VpFormPageScaffold([
      VpLogoHeader(
          onPressed: () {
            print(_controller.page);
            if (_controller.page > 0) {
              _controller.previousPage(
                  duration: const Duration(milliseconds: 800),
                  curve: Curves.easeInOutCubic);
            } else {
              Navigator.pop(context);
            }
          },
          pop: false),
      _pageContent,
      _pageIndicator
    ]);
  }
}

VpFormPageScaffold:

class VpFormPageScaffold extends StatelessWidget {
  VpFormPageScaffold(this.children);

  List<Widget> children;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomPadding: false,
        body: ConstrainedBox(
            constraints: BoxConstraints.tightFor(
                height: MediaQuery.of(context).size.height),
            child: VpGradientContainer(
                beginColor: initialGradientColor,
                endColor: endGradientColor,
                child: SafeArea(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: children)))));
  }
}

VpGradientContainer:

class VpGradientContainer extends StatelessWidget {
  const VpGradientContainer({this.child, this.beginColor, this.endColor});

  final Widget child;
  final Color beginColor;
  final Color endColor;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: child,
      height: double.infinity,
      width: double.infinity,
      padding: const EdgeInsets.all(40),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [beginColor, endColor],
        ),
      ),
    );
  }
}
1 Answers

Expanded widgets must be placed inside Flex widgets. You are using Expanded widget where it cannot be used. You'll have to remove Expanded widget from here :

final _pageContent = Expanded(
        child: Container(child: pageView, color: Colors.transparent), flex: 1);

Please checkout Flutter docs for Expanded Widget:

Expanded

A widget that expands a child of a Row, Column, or Flex so that the child fills the available space.

Using an Expanded widget makes a child of a Row, Column, or Flex expand to fill the available space along the main axis (e.g., horizontally for a Row or vertically for a Column). If multiple children are expanded, the available space is divided among them according to the flex factor.

An Expanded widget must be a descendant of a Row, Column, or Flex, and the path from the Expanded widget to its enclosing Row, Column, or Flex must contain only StatelessWidgets or StatefulWidgets (not other kinds of widgets, like RenderObjectWidgets).

Related