我正在构建一个页面,动态生成一些 View 。在我的情况下,显示的列表将根据用户输入(用作过滤器)进行更新。
使用 Text Widget 动态呈现时一切正常,但是当我尝试切换到 Column 或 Gridview 时,一切都出错了,并且出现以下错误
The following assertion was thrown building ServerGrid(dirty; state: _ServerGridState#59211289()):
I/flutter (14351): setState() or markNeedsBuild() called during build.
I/flutter (14351): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter (14351): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (14351): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (14351): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (14351): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (14351): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (14351): Overlay([LabeledGlobalKey<OverlayState>#774312152]; state: OverlayState#252339409(entries:
I/flutter (14351): [OverlayEntry#727022347(opaque: false; maintainState: false), OverlayEntry#1051156104(opaque:
I/flutter (14351): false; maintainState: true), OverlayEntry#280497357(opaque: false; maintainState: false),
I/flutter (14351): OverlayEntry#41791024(opaque: false; maintainState: true), OverlayEntry#444321361(opaque: false;
I/flutter (14351): maintainState: false), OverlayEntry#717668788(opaque: false; maintainState: true)]))
这是我目前得到的代码,
ServerGrid 这是引发错误的地方 导入'package:flutter/material.dart';
import'package:firebase_sandbox/models/server.dart';
class ServerGrid extends StatefulWidget {
ServerGrid({Key key, this.servers}) : super(key: key);
final servers;
@override
State createState() => new _ServerGridState();
}
class _ServerGridState extends State<ServerGrid> {
showInfos(serv){
showDialog(context: context, child: new AlertDialog(content: new Text(serv.toString())));
}
Widget buildServerTile(Server serv) {
return new InkWell(
onTap: showInfos(serv),
child :
new Column(
children: [
new CircleAvatar(child : new Image.network(serv.image)),
new Text(
serv.name,
style : new TextStyle(fontWeight: FontWeight.bold),
),
new Text(
serv.description,
style : new TextStyle(color : Colors.grey[500]),
),
],
),
);
}
List<Widget> buildGrid() {
var theGrid = <Widget>[];
for(Server s in widget.servers) {
theGrid.add(buildServerTile(s));
}
return theGrid;
}
@override
Widget build(BuildContext context) {
// return new Text("${widget.servers.toString()}"); //Working perfectly
return new GridView(
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
children: buildGrid(),
);
}
}
Controller 调用 View 进行更新:
filter(value) {
filteredList = _servers.where(
(serv) =>
serv.name.contains(value) || serv.tags
.where((tag) => tag.contains(value))
.length > 0
).toList();
print(filteredList);
setState(() {
serverList = new ServerGrid(servers : filteredList);
});
}
一直在搜索,但在构建 Gridview 时我不知道出了什么问题
最佳答案
您的 onTap
处理程序有问题。它试图在您的构建功能期间立即显示对话框,这是不允许的。
onTap: showInfos(serv),
将您的处理程序更改为函数:
onTap: () => showInfos(serv),
https://stackoverflow.com/questions/44485760/
相关文章:
http - 在flutter http请求中为所有请求设置默认 header 的最佳方法
api - 为 HTTPClient get() 请求设置超时
firebase - 从 Flutter 中的 Firestore 集合中获取所有内容
android - 如何在 Flutter 中创建可扩展的 ListView
flutter - 如何在不改变 AppBar 的情况下更改 TabBar 的背景颜色?
animation - 在 Flutter 中将 ListView 项目动画化为全屏
dart - Flutter 测试 MissingPluginException