Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:如何将数据添加到列表中?_Flutter - Fatal编程技术网

Flutter 颤振:如何将数据添加到列表中?

Flutter 颤振:如何将数据添加到列表中?,flutter,Flutter,我有一个从json数据生成聊天列表的代码 final jsonItems = (json.decode(jsonResponse.body))['messages'].cast<Map<String, dynamic>>(); List<Messages> usersList = jsonItems.map<Messages>((json) { return Messages.fromJson(json);

我有一个从json数据生成聊天列表的代码

      final jsonItems = (json.decode(jsonResponse.body))['messages'].cast<Map<String, dynamic>>();

      List<Messages> usersList = jsonItems.map<Messages>((json) {
        return Messages.fromJson(json);
      }).toList();
这是我用来显示聊天信息的生成器

FutureBuilder<List<Messages>>(
                    future: fetchJSONData(),
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        List<Messages> data = snapshot.data;
                        return _messagesListView(data);
                      } else if (snapshot.hasError) {
                        return Text("${snapshot.error}");
                      }
这是我的地图

enum MessageType {sent, received}
class Messages {
  MessageType status;
  String contactName;
  String message;
  String time;

  Messages({ this.status, this.message, this.contactName, this.time});

  factory Messages.fromJson(Map<String, dynamic> json) {
    return Messages(
        contactName: json['ownerName'],
        message: json['body'],
        time: json['createdAt'],
        status: MessageType.received
    );
  }
}

我已经完成了从WebSocket获取数据的工作,但是如何才能将新消息添加到从WebSocket接收到的列表中,并且该消息将显示在屏幕上

您需要在构建器中使用ConnectionState。查看此代码模板:当前构建器返回文本小部件,无需等待将来完成

return FutureBuilder<List<Messages>>(
     future: fetchJSONData(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        // future complete
        // if error or data is false return error widget
        if (snapshot.hasError || !snapshot.hasData) {
          return Text("${snapshot.error}");
        }

        // return data widget
         List<Messages> data = snapshot.data;
         return _messagesListView(data);

        // return loading widget while connection state is active
      } else
        return Center(
        child: CircularProgressIndicator());
             
    },
  );

在我在屏幕上做某件事之前,消息不会出现。你说的在屏幕上做某件事是什么意思?比方说,在我单击应用程序中的任何位置之前,消息不会出现。也许需要使用StreamBuilder?我是StreamBuilder的初学者,用于更改显示的数据。在您的情况下,FutureBuilder很好。您是否复制了我的代码段,而文本小部件未显示?
return FutureBuilder<List<Messages>>(
     future: fetchJSONData(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        // future complete
        // if error or data is false return error widget
        if (snapshot.hasError || !snapshot.hasData) {
          return Text("${snapshot.error}");
        }

        // return data widget
         List<Messages> data = snapshot.data;
         return _messagesListView(data);

        // return loading widget while connection state is active
      } else
        return Center(
        child: CircularProgressIndicator());
             
    },
  );