简体   繁体   English

Flutter :此错误出现在使用不包含 Scaffold 的上下文调用的 Scaffold.of() 。 当我尝试展示小吃店时

[英]Flutter : this error appear Scaffold.of() called with a context that does not contain a Scaffold. when i try to show a snackbar

  • I try to use a snackBar when the connectivity status changed, and the connectivity works fine when I use a print.当连接状态发生变化时,我尝试使用小吃店,当我使用打印时,连接工作正常。 But when I try to use a Scaffold.of(context).showSnackBar....,但是当我尝试使用 Scaffold.of(context).showSnackBar ....,

  • i got an error with ['Scaffold.of() called with a context that does not contain a Scaffold.我收到了一个错误 ['Scaffold.of() 在不包含 Scaffold 的上下文中调用。

No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of().从传递给 Scaffold.of() 的上下文开始,找不到任何 Scaffold 祖先。 This usually happens when the context provided is from the same StatefulWidget as that whose build function actually creates the Scaffold widget being sought.这通常发生在所提供的上下文来自与其构建函数实际创建正在寻找的 Scaffold 小部件相同的 StatefulWidget 时。

'] ']

  • I create a wrapper class which include a Widget as a constructor我创建了一个包装类,其中包含一个 Widget 作为构造函数


import 'package:Zabatnee/activities_app/enum/connectivity_status.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';


class ConnectivityWrapper extends StatefulWidget {
  final Widget childWidget;
  ConnectivityWrapper(this.childWidget);

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

class _ConnectivityWrapperState extends State<ConnectivityWrapper> {

 Widget _showOfflineSnakbar(){
Scaffold.of(context).showSnackBar(
                SnackBar(
                  content: 
                  
                  Text(
                    'No internet connection',
                  ),
                  duration: Duration(seconds: 3),
                ),
              );

            
           
  }

  @override
  void didChangeDependencies() {
 var connectionState = Provider.of<ConnectivityStatus>(context);

    if(connectionState == ConnectivityStatus.Offline){
          print('the internet is offline');
           _showOfflineSnakbar();

     
    }if(connectionState == ConnectivityStatus.Wifi || connectionState == ConnectivityStatus.Cellular){
    print('the internet is online');
           _showOfflineSnakbar();

    }
    super.didChangeDependencies();
  }
  @override
  Widget build(BuildContext context) {
    return widget.childWidget;
  }
}
  • and in main.dart I use the wrapper class as a HomePage and put inside it the real homeScreen:在 main.dart 中,我使用包装类作为 HomePage 并将真正的 homeScreen 放入其中:
 home: ConnectivityWrapper(
             CategoriesScreen(),
            
            ),

Well, it seems like you haven't constructed a Scaffold in any parent widgets.好吧,您似乎还没有在任何父小部件中构建 Scaffold。

Either do that, or have a look at the Flushbar package.要么这样做,要么看看 Flushbar 包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不包含脚手架的上下文调用的 Scaffold.of() - Scaffold.of() called with a context that does not contain a Scaffold 未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of() - Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold 处理手势时引发了以下断言: Scaffold.of() 使用不包含 Scaffold 的上下文调用 - The following assertion was thrown while handling a gesture: Scaffold.of() called with a context that does not contain a Scaffold Snackbar 和异常:scaffold.of() - Snackbar and exception: scaffold.of() 上下文的颤振脚手架“不包含脚手架” - Flutter scaffold of context giving "does not contain a Scaffold" 全局脚手架在颤动中显示 Snackbar - Global scaffold to show Snackbar in flutter Flutter Scaffold.of(context).openDrawer() 不起作用 - Flutter Scaffold.of(context).openDrawer() doesn't work 错误:没有名为“nullOk”的命名参数 Scaffold.of(context, nullOk: true) - Error: No named parameter with the name 'nullOk' Scaffold.of(context, nullOk: true) 尝试使用 Scaffold.of(context) 使用 openDrawer() 找到 2 而不是 0 时出现太多位置 arguments 错误 - Too many positional arguments error when trying to use openDrawer() using Scaffold.of(context) finding 2 instead of 0 如何在没有脚手架的情况下显示snackBar - How to show snackBar without Scaffold
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM