我正在使用react-native开发健身应用程序。目前,我所有的运动图像都存储在本地,但是我现在正在寻找从Firebase存储下载图像。在Firebase中,我已在图像文件夹中上传了21张图像,该文件夹中还有另外2个文件夹-foamRoll和拉伸。
我想做什么
我想将我的所有8张图像都从foamRoll文件夹(位于Firebase存储中)下载到我的Flatlist中。
我做了什么
在下面的代码段中,有我整个泡沫辊屏幕的代码。我保留了旧代码以获取本地存储的文件,还保留了使用useEffect(尝试)获取新文件的工作。但是我被困住了,文档没有帮助!
import React, { useState } from "react";
import {
StyleSheet,
Text,
View,
SafeAreaView,
FlatList,
Image,
TouchableOpacity,
Modal,
TouchableHighlight,
} from "react-native";
import { Colors } from "../colors/Colors";
import { firebase } from "../firebase/config";
const DATA = [
{
id: "1",
title: "Calves",
section: "Foam Roller",
image: require("../../assets/FR1.png"),
},
{
id: "2",
title: "Tibialis Anterior (Shin)",
section: "Foam Roller",
image: require("../../assets/FR2.png"),
},
{
id: "3",
title: "Quads",
section: "Foam Roller",
image: require("../../assets/FR3.png"),
},
{
id: "4",
title: "ITB (Outside of legs)",
section: "Foam Roller",
image: require("../../assets/FR4.png"),
},
{
id: "5",
title: "Glutes (Bum)",
section: "Foam Roller",
image: require("../../assets/FR5.png"),
},
{
id: "6",
title: "Traps (Back)",
section: "Foam Roller",
image: require("../../assets/FR6.png"),
},
{
id: "7",
title: "Lats (Side)",
section: "Foam Roller",
image: require("../../assets/FR7.png"),
},
{
id: "8",
title: "Pecs (Chest)",
section: "Foam Roller",
image: require("../../assets/FR8.png"),
},
];
export default function ExercisesScreen() {
const [modalVisible, setModalVisible] = useState(false);
const [currentItem, setCurrentItem] = useState({});
const [entities, setEntities] = useState([]);
const entityRef = firebase.firestore().collection("entities");
const storageRef = firebase.storage().ref("images/foamRoll");
useEffect(() => {
storageRef.list().then((result) => {
// Loop over each item
result.items.forEach((pics) => {
firebase
.storage()
.ref()
.child("images/foamRoll")
.getDownloadURL()
.then((url) => {
console.log(url);
//these url will be used to display images
});
});
});
}, []);
const Item = ({ item }) => (
<View style={styles.item}>
<Image source={item.image} style={styles.exerciseImage} />
<View style={styles.detailSection}>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.section}>{item.section}</Text>
</View>
<TouchableOpacity
style={styles.viewButton}
onPress={() => {
openSettingsModal(item);
}}
>
<Text style={styles.viewText}>View</Text>
</TouchableOpacity>
</View>
);
const openSettingsModal = (item) => {
setCurrentItem(item);
setModalVisible(!modalVisible);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.tabBackground}>
<Text style={styles.subTitle}>EXERCISES: FOAM ROLLER</Text>
</View>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
}}
>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text style={styles.modalText}>{currentItem.modalTitle}</Text>
<Text style={styles.modalText}>{currentItem.modalDesc}</Text>
<TouchableHighlight
style={{ ...styles.openButton, backgroundColor: "#2196F3" }}
onPress={() => {
setModalVisible(!modalVisible);
}}
>
<Text style={styles.textStyle}>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
<FlatList
data={DATA}
renderItem={({ item }) => <Item item={item} />}
keyExtractor={(item) => item.id}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({})