snapping_sheet
snapping_sheet copied to clipboard
Can not attach SnappingSheetController
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.
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
- create the SnappingSheetController()
- 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.
@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?
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.
);
} }
... code reproducing the bug
Same error, same everything
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.
Can you check my PR #74? I checked with the example above, everything is working fine now.