firebase - HostObject::get(propName:RNfirebase) 中的异常
问题描述
我正在学习如何使用 react-native(Android) 实现 Firestore。然后,我找到了“react-native-firebase”并陷入了这个异常。
Error:Exception in
HostObject::get(propName:RNFirebase):
java.lang.NoClassDefFoundError: Failed resolution
of: Lcom/google/firebase/FirebaseApp;
我已经在我的 gradle 中设置了 firebase(参考https://firebase.google.com/docs/android/setup/?authuser=0)
Firebase 数据库的规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
这是我的学习代码(参考https://www.youtube.com/watch?v=_GOI7h9ojr8)
import React,{ Component } from 'react';
import {
Flatlist,
Text,
TextInput,
TouchableHighlight,
Image,
View } from 'react-native';
import firebase from 'react-native-firebase';
export default class test extends Component {
constructor(props){
super(props);
this.state = ({
todoTask: [],
newTaskName: '',
loading: false
});
this.ref = firebase.firestore().collection('todo');
}
onPressAdd = () => {
this.ref.add({
taskName: this.state.newTaskName
}).then((data) => {
console.log('added data = ${data}');
this.setState({
newTaskName: '',
loading: true
});
}).catch((error) => {
console.log('error adding firestore document = ${error}');
this.setState({
newTaskName: '',
loading: true
});
});
}
render(){
return (
<View style={{flex: 1}}>
<Text>Hello</Text>
<TextInput style={{
height: 40,
width: 200,
margin: 10,
padding: 10,
borderColor: 'white',
borderWidth: 1,
color: 'white'
}}
keyboardType='default'
placeholderTextColor='white'
placeholder='Enter task name'
onChangeText={
(text) => {
this.setState({ newTaskName: text});
}
}
>
</TextInput>
<TouchableHighlight
style={{ marginRight: 10 }}
underlayColor='tomato'
onPress={this.onPressAdd}>
<Image
style={{ width: 35, height: 35 }}
source={require('./icon-add.png')}
>
</Image>
</TouchableHighlight>
<Flatlist
data={this.state.todoTask}
renderItem={({item, index}) => {
return(
<Text>{item.taskName}</Text>
);
}}
>
</Flatlist>
</View>
);
}
}
你能解释一下为什么会这样吗?非常感谢。
解决方案
You installed 'react-native-firebase' package so kindly note to remove(uninstall) it.
According to the docs:
First you need to install firebase package:
npm install --save firebase
Then import it using firebase/app:
// Firebase App (the core Firebase SDK) is always required and
// must be listed before other Firebase SDKs
import * as firebase from "firebase/app";
推荐阅读
- spring-batch - Spring批处理作业执行上下文
- visual-studio-code - VSCode 上的条件折叠?
- express - 如何在 express.js 中生成不透明令牌并将其与 react 应用程序一起使用
- google-sheets - 按顺序(上、下、左、右)将多张工作表的范围组合成单列
- sql - Oracle中的动态比较运算符
- javascript - 以编程方式创建 FORM - .submit 不会激活
- c# - 服务器客户端发送/接收多个客户端
- reactjs - 没有反应钩子的反应中的URL参数
- java - vscode - 无法使用 Gradle 分发“https://services.gradle.org/distributions/gradle-7.2-bin.zip”获取“GradleProject”类型的模型
- jakarta-mail - part.getFilename() 为拆分文件名返回 null