2017-06-11 101 views
1

我正在构建一个页面,动态生成一些视图。在我的情况下,显示的列表将在给定用户输入(用作过滤器)的情况下更新。颤振 - 无法构建,因为frawework已经构建

全部被工作完全使用文本组件被动态地呈现时,但是当我试图切换到一列或GridView的,一切都变了而一个得到了folowwing错误

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这是哪里引发错误 import'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(), 
     ); 
    } 
} 

控制器调用视图更新:

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的

回答

2

时我无法弄清楚什么是错的你有一个问题,你onTap处理器。它试图在构建函数期间立即显示对话框,这是不允许的。

onTap: showInfos(serv), 

更改您的处理程序是一个函数:

onTap:() => showInfos(serv), 
+0

哇... 我做这样愚蠢的错误羞愧。谢谢科林! –

+0

这是一个非常常见的错误,我也做了:) –

+0

哦,男人,我一直追着我的尾巴过去12小时,因为这... – sofakingforever