1

我正在开发一个需要使用 Firebase 进行 CRUD 操作的程序。我正在使用 Firestore 存储我的数据,并且我有一个名为“通知”的集合,如下所示: https ://i.stack.imgur.com/hCe8q.png

现在,我想做的是使用平面列表在屏幕上显示用户收到的不同通知。我想这样做,以便在将文档添加/删除/修改到集合“通知”中时实时更新。

我将基于此示例: https ://rnfirebase.io/firestore/usage-with-flatlists

但是我一直遇到这个错误: 错误:需要模块“node_modules/@react-native-firebase/firestore/lib/index.js”,它引发了异常:不变违规:本机模块不能为空。

(更长的错误,但我正在粘贴主要部分)

如果有人能帮我解决这个问题,我将不胜感激。谢谢你。

我的代码如下:

import { ActivityIndicator, FlatList, Text, SafeAreaView } from "react-native";
import firestore from "@react-native-firebase/firestore";

export default function NotificationScreen() {
  const [loading, setLoading] = useState(true); // Set loading to true on component mount
  const [notifications, setNotifications] = useState([]); // Initial empty array of users

  useEffect(() => {
    const subscriber = firestore()
      .collection("notifications")
      .onSnapshot.forEach((querySnapshot) => {
        const notifications = [];
        querySnapshot.forEach((documentSnapshot) => {
          notifications.push({
            ...documentSnapshot.data(),
            key: documentSnapshot.id,
          });
        });
        setNotifications(notifications);
        setLoading(false);
      });
    return () => subscriber();
  }, []);

  if (loading) {
    return <ActivityIndicator />;
  }

  return (
    <FlatList
      data={notifications}
      renderItem={({ item }) => (
        <SafeAreaView
          style={{
            flex: 1,
          }}
        >
          <Text>Notification ID: {item.id}</Text>
          <Text>Notification time: {item.time}</Text>
        </SafeAreaView>
      )}
    />
  );
}```


  [1]: https://i.stack.imgur.com/Rhpgv.png
4

0 回答 0