保持从MySQL提取新数据的最佳方法 [英] Best way to keep fetching new data from MySQL

查看:52
本文介绍了保持从MySQL提取新数据的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些建议或帮助.我正在尝试做一个小型的实时聊天应用程序,我一直在寻找的大多数教程基本上都使用firebase,但是我在小型项目的其他部分已经使用MySQL,我不确定我是否理解 Stream 正确,但是据我所知,我确实一直在获取数据,我尝试了一下,但是它没有更新新数据,除了 StreamBuilder 有点难以使用之外,我选择了使用常规http的最简单方法,并使用将在页面启动时运行的函数来显示它,如下所示:

I need some suggestions or help please. I am trying to do a small real time chat application, most tutorials that I have been looking basically use firebase, but I have been using MySQL already for other parts of my small projects, I am not sure if I understood Stream correctly, but as far as I know I think that the data does keep fetching, I tried it but it didn't update the new data, and besides StreamBuilder being a little hard to use, I went for the easiest method using a regular http and displaying it by using a function which will be run at the initiation of page like this:

void initState() {
    super.initState();
    _getChat();
  }

_getChat() {
    Pulling.getChatsContents().then((value) {
      setState(() {
        _chatContent = value;
        _chatLength = "${value.length}";
      });
    });
  }

将它们存储在这里:

List<Chats> _chatContent;
String _chatLength = '0';

其中 List< Chats> 如下:

class Chats {
  final String senderid;
  final String msgcontent;

  Chats(
      {this.senderid,
      this.msgcontent});

  factory Chats.fromJson(Map<String, dynamic> json) {
    return Chats(
      senderid: json['sender_id'] as String,
      msgcontent: json['msg_content'] as String,
    );
  }
}

Pulling 是:

class Pulling {
  static const ROOT = 'https://example.com/chatsContent.php';
  static StreamController streamChat = StreamController();

  static Future<List<Chats>> getChatsContents() async {
    try {
      var myUser = '2';
      var map = Map<String, dynamic>();
      map['user'] = myUser;
      final response = await http.post(ROOT, body: map);
      if (200 == response.statusCode) {
        final chatsParsed =
            json.decode(response.body).cast<Map<String, dynamic>>();
        List<Chats> listChat =
            chatsParsed.map<Chats>((json) => Chats.fromJson(json)).toList();
        return listChat;
      } else {
        return List<Chats>();
      }
    } catch (e) {
      return List<Chats>();
    }
  }
}

因此,基本上所有这些我都在获取数据并显示,但是当添加新数据时,我没有得到更新,也许可以使用 Stream 来做到这一点,但是我将它与 http 结合起来实际上是很混乱的,所以我在考虑是否有可能,如果将它添加到void initState中,对于应用程序是否有效:

So basically with all these I am getting the data and displaying, but when new data is added, I do not get the update, there may be a way to do it with Stream, but I am actually quite confused combining it with http, so I was thinking if it is possible and if it is efficient for the app if I added this to the void initState:

_timer = Timer.periodic(Duration(seconds: 1), (timer) => _getChat());

还要在开始时调用 _timer :

Timer _timer;

谢谢大家阅读所有这些内容并为我提供帮助!

Thank you guys for reading all this and helping me!

更新

我显示获取的数据的方式如下:

The way that I display the fetched data is as follows:

return Column(
children: [
for (var i = 0; i < int.parse(_chatLength); i++)
Container(
child: Text(_chatContent[i].msgcontent),
),
]);

推荐答案

在我看来,您应该使用WebSocket而不是HTTP来立即获取新数据.

In my opinion you should use WebSocket instead of HTTP in order to fetch new data instantly.

这篇关于保持从MySQL提取新数据的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆