akshathjain / sliding_up_panel

A draggable Flutter widget that makes implementing a SlidingUpPanel much easier!
https://pub.dartlang.org/packages/sliding_up_panel
Other
1.36k stars 379 forks source link

Error with assigning ScrollController to class member variable #257

Closed JakeHadley closed 3 years ago

JakeHadley commented 3 years ago

I'm trying to initialize a class variable of the ScrollController object given in the function of the panelBuilder. My code is like this:

class MapPageState extends State<MapPage> {
  late final ScrollController _sc;

  //build stuff that returns _slidingPanel

  Widget _slidingPanel(BusDataState state) {
     return SlidingUpPanel(
        controller: _pc,
        panelBuilder: (ScrollController sc) {
          _sc = sc;
          return viewAnimatedSwitcher(); //trying to avoid passing in here and passing into all child functions
                                         //to get access to the controller
        },
        parallaxEnabled: true,
        parallaxOffset: 0.5,
        defaultPanelState: PanelState.CLOSED,
        backdropEnabled: true,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(45),
          topRight: Radius.circular(45),
        ),
        body: _map(state),
      );
  }

But I get an error saying

LateInitializationError: Field '_sc@122271042' has already been initialized.

Is there a good way to do this so that I don't have to pass the scroll controller object through 3-4 functions just to get it to where it needs to be used?

JakeHadley commented 3 years ago

I fixed this by changing the variable from

late final ScrollController _sc;

to

late ScrollController _sc;