snapping_sheet icon indicating copy to clipboard operation
snapping_sheet copied to clipboard

Can not attach SnappingSheetController

Open maitycyrus opened this issue 3 years ago • 7 comments

I always get the error:

SnappingSheet must be attached before calling any function from the controller. Pass in the controller to the snapping sheet widget to attached. Use [isAttached] to check if it is attached or not.
'package:snapping_sheet/src/snapping_sheet_widget.dart':
Failed assertion: line 418 pos 7: 'isAttached'

Also, I think there is a mistake in your readme of how to attach the SnappingSheetController. You call the constructor of the controller twice. I tried it both way, and it did not work.

maitycyrus avatar May 23 '21 11:05 maitycyrus

I have the code set up like the example and I got the same error maitycyrus mentioned: "SnappingSheet must be attached before calling any function from the controller".

I readjusted my code where I then

  1. create the SnappingSheetController()
  2. create the SnappingSheet, and attach the controller

` @override void initState() {

_snappingSheetController = SnappingSheetController();

this.sheet = SnappingSheet(
  controller: _snappingSheetController, // snappingSheetController attached here
  snappingPositions: [
    SnappingPosition.factor(
      positionFactor: POSITION_FACTOR_START,

... ... }`

Now I"m getting a Null check operator on a null value error:

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: Null check operator used on a null value #0 _SnappingSheetState.sheetSize package:snapping_sheet/src/snapping_sheet_widget.dart:307 #1 _SnappingSheetState.initState.. package:snapping_sheet/src/snapping_sheet_widget.dart:192 #2 State.setState package:flutter/…/widgets/framework.dart:1267 #3 _SnappingSheetState.initState. package:snapping_sheet/src/snapping_sheet_widget.dart:190 #4 _rootRunUnary (dart:async/zone.dart:1362:47) #5 _CustomZone.runUnary (dart:async/zone.dart:1265:19) #6 _FutureListener.handleValue (dart:async/future_impl.dart:152:18) #7 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:704:45) #8 Future._propagateToListeners (dart:async/future_impl.dart:733:32) #9 Future._complete (dart:async/future_impl.dart:530:7) #10 new Future.delayed. (dart:async/future.<…>

redmacdev1988 avatar May 27 '21 02:05 redmacdev1988

@maitycyrus You are right regarding the fault in the README, you should pass in the variable instead of creating a new controller. If you got the time, could you send the code for when you are using the controller?

@redmacdev1988 Seems that I can not replicate the error. Could you send the code for the entire widget that contains the SnappingSheet widget and uses the controller?

AdamJonsson avatar May 28 '21 16:05 AdamJonsson

import 'package:flutter/material.dart'; import 'package:snapping_sheet/snapping_sheet.dart';

void main() { runApp(MyApp()); }

class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData(

    primarySwatch: Colors.blue,
  ),
  home: MyHomePage(title: 'Flutter Demo Home Page'),
);

} }

class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override _MyHomePageState createState() => _MyHomePageState(); }

class _MyHomePageState extends State<MyHomePage> {

@override Widget build(BuildContext context) {

SnappingSheetController snappingSheetController = SnappingSheetController();
return Scaffold(
  appBar: AppBar(

    title: Text(widget.title),
  ),
  body: SnappingSheet(
    // Connect it to the SnappingSheet
    controller: snappingSheetController,

    grabbingHeight: 200,
    grabbing: GestureDetector(
      onTap: (){
        print('Controller is not working!');
        snappingSheetController.snapToPosition(
          SnappingPosition.factor(positionFactor: 0.75),
        );
      },
      child: Container(
        height: 200,
          color: Colors.blue
      ),
    ),
    sheetBelow: SnappingSheetContent(
      draggable: true,
      child: Container(color: Colors.red),
    ),
  ), // This trailing comma makes auto-formatting nicer for build methods.
);

} }

maitycyrus avatar Jun 04 '21 12:06 maitycyrus

... code reproducing the bug

maitycyrus avatar Jun 04 '21 13:06 maitycyrus

Same error, same everything

Lix-ai avatar Jun 16 '21 18:06 Lix-ai

I think the problem is the initState method in _SnappingSheetState it does not get called when hot reloading. It works if I run on restart.

maitycyrus avatar Jul 13 '21 12:07 maitycyrus

Can you check my PR #74? I checked with the example above, everything is working fine now.

akbarpulatov avatar Aug 28 '21 06:08 akbarpulatov