简体   繁体   English

Flutter:StreamBuilder stream 未更新

[英]Flutter: StreamBuilder stream not updating

My StreamBuilder didn't refresh after changes to the stream, and also the stream does not update too, when fetching data from Firebase and store it to local SQLite database; My StreamBuilder didn't refresh after changes to the stream, and also the stream does not update too, when fetching data from Firebase and store it to local SQLite database;

And here is my code for listen to data changes from Firebase and then write those new chats to local :这是我的代码,用于收听 Firebase 的数据更改,然后将这些新聊天写入本地

    /// LISTEN TO THE STREAM FROM FIREBASE AND THEN WRITE THE NEW MESSAGES TO THE LOCAL DATASE

  // Fetch normal messages
      firestore.fetchMessagesStream(chatRoomId).listen((event) async {
        for (QueryDocumentSnapshot message in event.docs) {
           _database.insert(
            'Messages',
            message.data()!,
            conflictAlgorithm: sql.ConflictAlgorithm.replace,
          );
        }
      });
      // Fetch calls
      firestore.fetchCallsStream(chatRoomId).listen((event) async {
        for (QueryDocumentSnapshot call in event.docs) {
          _database.insert(
            'Calls',
            call.data()!,
            conflictAlgorithm: sql.ConflictAlgorithm.replace,
          );
      // Fetch posts
      firestore.fetchPostsStream(chatRoomId).listen((event) async {
        for (QueryDocumentSnapshot post in event.docs) {
          _database.insert(
            'Posts',
            post.data()!,
            conflictAlgorithm: sql.ConflictAlgorithm.replace,
          );
        }
      });

And here the code for fetching data from the Local SQLite database :这里是从本地 SQLite 数据库中获取数据的代码:

/// STREAM FOR LISTENING THE CHANGES IN THE LOCAL DATABASE

Rx.combineLatest3(
      _database.query(
       'Messages',
        where: 'chatRoomId = ?',
        whereArgs: [chatRoomId],
        ).asStream(), // Returns the stream of the Messages Table in local databases
      _database.query(
        'Posts',
        where: 'chatRoomId = ?',
        whereArgs: [chatRoomId],
     ).asStream(), // Returns the stream of the Posts Table in local databases
      _database.query(
        'Calls',
        where: 'chatRoomId = ?',
        whereArgs: [chatRoomId],
       ).asStream(), // Returns the stream of the Calls Table in local databases
      (List<Map<String, dynamic>> streamingMessages,
          List<Map<String, dynamic>> streamingPosts,
          List<Map<String, dynamic>> streamingCalls) {
        /// VERY IMPORTANT: THE FOLLOWING PRINT STATEMENT WILL BE PRINT OUT EVERYTIME A NEW CHAT ARRIVE
        /// VERY IMPORTANT: THE FOLLOWING PRINT STATEMENT WILL BE PRINT OUT EVERYTIME A NEW CHAT ARRIVE
        /// VERY IMPORTANT: THE FOLLOWING PRINT STATEMENT WILL BE PRINT OUT EVERYTIME A NEW CHAT ARRIVE
        /// VERY IMPORTANT: THE FOLLOWING PRINT STATEMENT WILL BE PRINT OUT EVERYTIME A NEW CHAT ARRIVE
        /// VERY IMPORTANT: THE FOLLOWING PRINT STATEMENT WILL BE PRINT OUT EVERYTIME A NEW CHAT ARRIVE
        print('MySqlite: chat stream changes!');

        final List<dynamic> contents = [...streamingMessages, ...streamingPosts, ...streamingCalls];

        return contents;
      },
    );
  }

The expected timeline when sent a new message will be:发送新消息时的预期时间线将是:

User sent a message --> trigger changes in Firebase --> trigger the LOCAL SQLite to add new data --> Because our SQLite have added new chats, so our ChatScreen should refresh & also a new debug message: 'MySqlite: chat stream changes.' User sent a message --> trigger changes in Firebase --> trigger the LOCAL SQLite to add new data --> Because our SQLite have added new chats, so our ChatScreen should refresh & also a new debug message: 'MySqlite: chat stream变化。' should be printed out again in the console since the ChatScreen is listening to the SQLite Database Stream.应该在控制台中再次打印出来,因为 ChatScreen 正在侦听 SQLite 数据库流。

But the actual result is:但实际结果是:

User sent a message --> trigger changes in Firebase successfully --> but I DIDN'T see the screen being refreshed NOR new debug message in the console...用户发送了一条消息 --> 成功触发 Firebase 中的更改 --> 但我没有看到屏幕正在刷新,也没有在控制台中看到新的调试消息......

I've been struggle with this issue for days, and I don't know why the result is not what I want, if I let the ChatScreen to directly listen to Firebase, it works!我已经为这个问题苦苦挣扎了好几天,我不知道为什么结果不是我想要的,如果我让 ChatScreen 直接听 Firebase,它可以工作! * *

UPDATE:更新:

I just found out that if I rebuild the ChatScreen (pop the screen, and then open it again), or setState when sending a new message, I can see the new message, it proves that the Message did go into the SQLite database after sent, but it just did not trigger the StreamBuilder.刚发现如果我重建ChatScreen(弹屏,然后再打开),或者发送新消息的时候setState,可以看到新消息,证明这个Message在发送后确实是go进入SQLite数据库,但它只是没有触发 StreamBuilder。 So it might be something wrong with the Fetching Stream.因此,获取 Stream 可能有问题。

ANSWER:回答:

I just found out that the SQLite.query function cannot be fetched as a Stream, and I thought I can by using "asStream" method, but this does not do anything, it is a missed feature that SQLite package didn't implement yet, so I add the sqlbrite package that works as a wrapper of the original SQLite package, and it has some additional feature such as querying data as a Stream. I just found out that the SQLite.query function cannot be fetched as a Stream, and I thought I can by using "asStream" method, but this does not do anything, it is a missed feature that SQLite package didn't implement yet, so I add the sqlbrite package that works as a wrapper of the original SQLite package, and it has some additional feature such as querying data as a Stream. ^_^ ^_^

I just found out that the SQLite.query function cannot be fetched as a Stream, and I thought I can by using "asStream" method, but this does not do anything, it is a missed feature that SQLite package didn't implement yet, so I add the sqlbrite package that works as a wrapper of the original SQLite package, and it has some additional feature such as querying data as a Stream. I just found out that the SQLite.query function cannot be fetched as a Stream, and I thought I can by using "asStream" method, but this does not do anything, it is a missed feature that SQLite package didn't implement yet, so I add the sqlbrite package that works as a wrapper of the original SQLite package, and it has some additional feature such as querying data as a Stream. ^_^ ^_^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM