[英]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 时。
'] ']
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;
}
}
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.