Scaffold.of(context) == null

778 views
Skip to first unread message

Kevin Segaud

unread,
May 24, 2016, 4:45:56 AM5/24/16
to Flutter Dev
Hi,

I'm trying to add a snackbar after an user action but Scaffold.of(context) == null so i can't use the showSnackBar method.
I'm my build i have a Scaffold Widget.

here is the error :
I/flutter : ══╡ EXCEPTION CAUGHT BY GESTURE LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter : The following NoSuchMethodError was thrown while routing a pointer event:
I/flutter : The null object does not have a method 'showSnackBar'.
I/flutter : NoSuchMethodError: method not found: 'showSnackBar'
I/flutter : Receiver: null
I/flutter : Arguments: [Instance of 'SnackBar']
I/flutter : When the exception was thrown, this was the stack:
I/flutter : #0      Object._noSuchMethod (dart:core-patch/object_patch.dart:42)
I/flutter : #1      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
I/flutter : #2      AddQuoteScreenState._onPressSend (/home/kleak/Work/GeoQuote/lib/src/screen/addquote_screen.dart:57)
I/flutter : #3      AddQuoteScreenState._buildBody.<anonymous closure> (/home/kleak/Work/GeoQuote/lib/src/screen/addquote_screen.dart:40)
I/flutter : #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:161)
I/flutter : #5      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:116)
I/flutter : #6      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:62)
I/flutter : #7      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:198)
I/flutter : #8      PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:53)
I/flutter : #9      BindingBase&SchedulerBinding&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:115)
I/flutter : #10     BindingBase&SchedulerBinding&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:93)
I/flutter : #11     BindingBase&SchedulerBinding&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:70)
I/flutter : #12     BindingBase&SchedulerBinding&GestureBinding._handlePointerPacket (package:flutter/src/gestures/binding.dart:42)
I/flutter : #13     _dispatchPointerPacket (file:///b/build/slave/Linux_Engine/build/src/out/android_debug/gen/sky/bindings/hooks.dart:43)
I/flutter : Event:
I/flutter :   PointerUpEvent(Point(336.7, 223.7))
I/flutter : ════════════════════════════════════════════════════════════════════════════════════════════════════
 

Thanks

Kris Giesing

unread,
May 24, 2016, 11:28:33 AM5/24/16
to Kevin Segaud, Flutter Dev
Hi Kevin,

It looks like you are calling build()-related methods during a gesture event callback?

The build() pipeline is controlled by the Flutter framework; build() methods can't be called outside of that pipeline sequence. In particular, you're not going to have a context to work with, which would explain why Scaffold.of(context) is returning null.

Generally, a gesture handler callback will cause the UI to rebuild via a call to setState() on some stateful widget in the tree.  The setState() handling will construct the pipeline and call build() across the right part of the widget tree to get the UI to rebuild.

We've been trying to make the Flutter error messages a little more self explanatory; I wonder if there's something we could do here to improve things. For example, maybe the *.of() methods could emit a helpful message when the context input argument is null.  I'll log an issue to track this.

Thanks,

- Kris

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To post to this group, send email to flutt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/62e8546b-96e6-4246-9b64-d784a7f607cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian Hickson

unread,
May 24, 2016, 11:34:33 AM5/24/16
to Kevin Segaud, Flutter Dev
You're probably calling this from the same object that actually creates the scaffold, so its context is "above" the scaffold and "Scaffold.of()" doesn't find it. There's a couple of easy solutions:

1. You can split your widget in two, and put the logic that does the snack bar stuff in the widget that's below the Scaffold. Another way to do this is to use a Builder widget in your build function and put the logic for showing the snackbar into the Builder's build function (since its context will see the Scaffold).

2. You can put a GlobalKey<ScaffoldState> on the Scaffold, and when you need to refer to the scaffold, instead of doing Scaffold.of() you can use key.currentState.

I think Kris is misreading the stack, it doesn't look like build itself is being called incorrectly. It's just a closure that's being set as the handler in the build function, which is fine. Please do file a bug though, we should certainly make this clearer.

-- 
Ian Hickson

--

Kevin Segaud

unread,
May 24, 2016, 11:34:38 AM5/24/16
to Kris Giesing, Flutter Dev

Thanks for the explaination
And yes it's when i press a button :)
Is there a way to show a snackbar when i click on a button or after more precisely i press on the button it make a request on a server and after the reponse show the snackbar ?

Kevin Segaud

unread,
May 24, 2016, 11:49:27 AM5/24/16
to Ian Hickson, Flutter Dev

Yes it's in the same widget.
Will try what you propose.
Maybe explain a bit more how to use .of methods should be suffisant, like :
Only find object that is not in the same widget :)

Thanks a lot

Kris Giesing

unread,
May 24, 2016, 12:23:09 PM5/24/16
to Ian Hickson, Kevin Segaud, Flutter Dev
Whoops! You're right. Sorry for the confusion.

Kris Giesing

unread,
May 25, 2016, 12:02:05 AM5/25/16
to Ian Hickson, Kevin Segaud, Flutter Dev
Looks like Ian filed this to track better documentation on the topic:


Thanks Ian :)
Reply all
Reply to author
Forward
0 new messages