gpt4 book ai didi

flutter - 'LabeledGlobalKey>'类型不是'GlobalKey 类型的子类型

转载 作者:行者123 更新时间:2023-12-03 04:59:01 28 4
gpt4 key购买 nike

自Common.dart起,我试图在首页上显示SnackBar,但 flutter 地向我发送如下错误:

通过gesture捕获到异常:

The following assertion was thrown while handling a gesture:
type 'LabeledGlobalKey<State<StatefulWidget>>' is not a subtype of type 'GlobalKey<ScaffoldState>'

测试:
-我尝试用 showSnackbar替换 return方法,并且错误停止,但 return String不起作用。

代码 HomePage.dart:
import 'package:flutter/material.dart';
import 'package:yakoa/custom_librairies/Common.dart';

class HomePage extends StatefulWidget {
@override
_HomePage createState() => _HomePage();
}

class _HomePage extends State<HomePage> {
GlobalKey _scaffoldkey = new GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldkey,
body: Padding(
padding: EdgeInsets.only(top: 200),
child: Column(
children: <Widget>[
Text("Element 1",
style: TextStyle(
fontSize: 35, color: Theme.of(context).primaryColor)),
buildSeparator(MediaQuery.of(context).size.width),
Text("Element 1",
style: TextStyle(
fontSize: 35, color: Theme.of(context).primaryColor)),
buildSeparator(MediaQuery.of(context).size.width)
],
)),
floatingActionButton: RaisedButton(
child: Text("Click"),
onPressed: () {
showSnackbar(_scaffoldkey,"yes");
},
),
);
Common.dart:
void showSnackbar(GlobalKey<ScaffoldState> scaffoldKey, String text) {
final snackBar = SnackBar(content: Text(text));
scaffoldKey.currentState.hideCurrentSnackBar();
scaffoldKey.currentState.showSnackBar(snackBar);
}

谢谢,
é

最佳答案

添加GlobalKey的类型:

  GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();

关于flutter - 'LabeledGlobalKey<State<StatefulWidget>>'类型不是'GlobalKey <ScaffoldState>类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59981655/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com