Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Can not attach SnappingSheetController #63

Open
maitycyrus opened this issue May 23, 2021 · 7 comments
Open

Can not attach SnappingSheetController #63

maitycyrus opened this issue May 23, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@maitycyrus
Copy link

maitycyrus commented May 23, 2021

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.

@redmacdev1988
Copy link

redmacdev1988 commented May 27, 2021

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.<…>

@AdamJonsson
Copy link
Owner

@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 AdamJonsson added the bug Something isn't working label May 28, 2021
@maitycyrus
Copy link
Author

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 {

@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
Copy link
Author

... code reproducing the bug

@Lix-ai
Copy link

Lix-ai commented Jun 16, 2021

Same error, same everything

@maitycyrus
Copy link
Author

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.

@akbarpulatov
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants