How to show SnackBar in Flutter -


i want show snackbar widget when bottom tab clicked. trying show as:

scaffold.of(context).showsnackbar(new snackbar(                 content: new text("live clicked"),               )); 

however app throws following exception:

i/flutter ( 4965): following assertion thrown while handling gesture: i/flutter ( 4965): scaffold.of() called context not contain scaffold. i/flutter ( 4965): no scaffold ancestor found starting context passed scaffold.of(). i/flutter ( 4965): happens when context provided same statefulwidget build i/flutter ( 4965): function creates scaffold widget being sought. i/flutter ( 4965): there several ways avoid problem. simplest use builder context i/flutter ( 4965): "under" scaffold. example of this, please see documentation scaffold.of(): i/flutter ( 4965):   https://docs.flutter.io/flutter/material/scaffold/of.html i/flutter ( 4965): more efficient solution split build function several widgets. introduces i/flutter ( 4965): new context can obtain scaffold. in solution, have outer widget i/flutter ( 4965): creates scaffold populated instances of new inner widgets, , in these inner i/flutter ( 4965): widgets use scaffold.of(). i/flutter ( 4965): less elegant more expedient solution assign globalkey scaffold, use i/flutter ( 4965): key.currentstate property obtain scaffoldstate rather using scaffold.of() function. i/flutter ( 4965): context used was: i/flutter ( 4965):   myhomepage(state: _myhomepagestate(603645610)) 

although exception self-explanatory. don't understand why occurring since myhomepage widget has scaffold in it.

complete code:

import 'package:flutter/material.dart';  void main() {   runapp(new myapp()); }  class myapp extends statelesswidget {   // widget root of application.   @override   widget build(buildcontext context) {     return new materialapp(       title: 'app name',       theme: new themedata(         primaryswatch: colors.blue,       ),       home: new myhomepage(title: 'app name'),     );   } }  class myhomepage extends statefulwidget {   myhomepage({key key, this.title}) : super(key: key);    final string title;    @override   _myhomepagestate createstate() => new _myhomepagestate(); }  class _myhomepagestate extends state<myhomepage> {   var bottombarlabels = [     new destinationlabel(         icon: new icon(icons.live_tv), title: new text("live")),     new destinationlabel(         icon: new icon(icons.date_range), title: new text("matches")),   ];    @override   widget build(buildcontext context) {     void _handlebottomnavigationbartap(int newvalue) {       switch (newvalue) {         case 0:           print("live clicked");           scaffold.of(context).showsnackbar(new snackbar(                 content: new text("live clicked"),               ));           break;         case 1:           print("matches clicked");           break;       }     }      return new scaffold(       key: new key("homepage"),       appbar: new appbar(         title: new text(config.title),       ),       bottomnavigationbar: new bottomnavigationbar(           labels: bottombarlabels, ontap: _handlebottomnavigationbartap),     );   } } 

you do have scaffold, not above context of myhomepage. scaffold child of myhomepage, while doing scaffold.of(context) trying access closest parent scaffold. , since have none, crash.

you should wrap bottomnavigationbar new class. , use widget's context scaffold.of(context).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -