在statefullWidget flutter-web中调用类时出错

时间:2019-07-05 07:40:44

标签: flutter dart flutter-web

当我在浏览器中使用此代码输出时,断言失败:不正确。通常是这样的,因为数据没有发送到列表视图,对吗?但是哪里出了问题?

  
    

══╡小工具库引起的异常CA ═════════════════════     引发以下断言以构建ItemActivity(dirty):断言     失败:不是真的

  

这是我调用API时的代码

          FutureBuilder(
              future: UserController.getActivity(_selectedUser),
              builder: (context, snapshot) {
                if (snapshot.hasError) print(snapshot.error);
                return snapshot.hasData
                    ? new ItemActivity(
                        list: snapshot.data,
                      )
                    : new Center(
                        child: CircularProgressIndicator(),
                      );
              }),

此代码我在页面底部,确实在窗口小部件之外

 class ItemActivity extends StatelessWidget {
  final List list;
  ItemActivity({this.list});
  @override
  Widget build(BuildContext context) {
    return new ListView.builder(
      itemCount: list == null ? 0 : list.length,
      itemBuilder: (context, i) {
        Map<String, dynamic> activity = list[i]['activity'];
        return new Container(
          padding: EdgeInsets.all(10.0),
            child: Container(
              child: Card(
                  child: new ListTile(
                title: Text(activity.containsKey("project")
                    ? activity["project"]
                    : "-"),
                subtitle: Text("created at : ${list[i]['created_at']}"),
              )),
            ),
        );
      },
    );
  }
}

1 个答案:

答案 0 :(得分:0)

尝试将您的ItemActivity()的构造函数更改为此:

ItemActivity({Key key, this.list}) : super(key: key);