【问题标题】:Flatlist doesn't display data from firebaseFlatlist 不显示来自 firebase 的数据
【发布时间】:2021-06-26 00:10:28
【问题描述】:

我正在尝试在 Flatlist 中显示一项,但它没有显示任何内容。甚至 Flatlist 之外的项目也不会显示。我通常不使用 Class 组件,所以也许我错过了什么?

static navigationOption = ({ navigation }) => {
        return {
          scannedkey: navigation.getParam("itemId", null)
        }
      }
     
   
    constructor(props){
        super(props);
        this.state={ 
        productlist:[],

        
        } }
        
    async componentDidMount(scannedkey){
        
        firebase.database().ref(`product/${scannedkey}`).on(
          "value",
          (snapshot) => {
            var list = [];
            snapshot.forEach((child) => {
              list.push({
                key: child.key,
                title: child.title,
                //details: child.val().details,
                //price: child.val().price
              });
            });
        
            this.setState({ productlist: list });
          },
          (error) => console.error(error)
        );
    }
  componentWillUnmount() {
      if (this.valuelistener_) {
        this.valueRef_.off("value", this.valuelistener_)
      }}
    
  render() {
    console.log(this.state.productlist)
 return(
     <View style={styles.container}>
       <Text>Hey</Text>
      <Text>{this.state.productlist.title}</Text>
     </View>
 );}}

这是我得到的错误:

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 392608ms)
at node_modules\react-native\Libraries\LogBox\LogBox.js:117:10 in registerWarning
at node_modules\react-native\Libraries\LogBox\LogBox.js:63:8 in warnImpl
at node_modules\react-native\Libraries\LogBox\LogBox.js:36:4 in console.warn
at node_modules\expo\build\environment\react-native-logs.fx.js:18:4 in warn
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:226:6 in setTimeout
at node_modules\@firebase\database\dist\index.esm.js:99:8 in MemoryStorage

如果您能帮我找到解决方法,我将不胜感激。

【问题讨论】:

  • 错字。您正在设置 produclist 而不是 productlist
  • @jnpdx 修复了错字仍然无效我还添加了我从控制台日志中获得的内容。

标签: firebase react-native firebase-realtime-database react-native-flatlist react-native-firebase


【解决方案1】:

在您的 on('value') 处理程序中,您有一个错字:

this.setState({ produclist: list });

应该是

this.setState({ productlist: list });

on(...) 方法还接受一个错误处理回调,您应该附加该回调,以便获取有关任何数据库错误的信息:

.on(
  "value",
  (snapshot) => {
    var list = [];
    snapshot.forEach((child) => {
      const childData = child.val();
      list.push({
        key: child.key,
        title: childData.title,
        //details: childData.details,
        //price: childData.price
      });
    });

    this.setState({ productlist: list });
  },
  (error) => console.error(error)
);

还有一点,on(...) 方法返回一个回调,您应该在 componentWillUnmount 中使用该回调,以便正确销毁您的元素:

componentDidMount(scannedkey) {
  this.valueRef_ = firebase
    .database()
    .ref(`product/${scannedkey}`);
  this.valuelistener_ = this.valueRef_
    .on("value", ...)
}

componentWillUnmount() {
  if (this.valuelistener_) {
    this.valueRef_.off("value", this.valuelistener_)
  }
}

【讨论】:

  • 谢谢您添加了所询问的内容,但仍然不显示我猜可能是我的 Flatlist。
  • @MayWheather 您还没有将错误处理程序附加到on('value')(如本答案所示),这可能会显示您的问题。
  • 对不起,我更新了代码并发布了一条我收到的消息。
  • @MayWheather productlist 的错字又回来了。
  • 感谢您再次指出。现在它不显示错误,但它仍然显示数组为空并给我一条警告消息。
猜你喜欢
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
相关资源
最近更新 更多