I tried all the options, but I couldn't display images from the database. The question is why are the images not displayed?
Data
data class ArtWallpaper (
val Name: String = "",
val Link: String = "",
val Id: String = ""
)
FirebaseModule
@Module
@InstallIn(SingletonComponent::class)
object FirebaseModule {
@Provides
@Singleton
fun providesFirebaseFirestore(): FirebaseFirestore = Firebase.firestore
}
RepositoryImpl
override fun getWallpapersRealtime(): Flow<Resource<List<ArtWallpaper>>> = callbackFlow {
val docRef = db.collection("Category")
val listener = docRef.addSnapshotListener { snapshot, e ->
if (e != null) {
/* TODO: Handle the error */
return@addSnapshotListener
}
if (snapshot != null) {
val cars = snapshot.toObjects<ArtWallpaper>(ArtWallpaper::class.java)
trySend(Resource.Success<List<ArtWallpaper>>(data = cars)).isSuccess
}
}
awaitClose {
listener.remove()
close() n
}
}
MainViewModel
@HiltViewModel
class MainViewModel @Inject constructor(
private val artWallpapersRepository: ArtWallpapersRepository
) : ViewModel() {
// List of cars
private var _cars = mutableStateOf<List<ArtWallpaper>>(emptyList())
val cars: State<List<ArtWallpaper>> = _cars
init {
// The function will be called when the viewModel gets called
artWallpapersRepository.getWallpapersRealtime()
// onEach will trigger whenever a new value is retrieved
.onEach { resource ->
when (resource) {
is Resource.Error<*> -> {
/* TODO: Handle the error */
}
is Resource.Success<*> -> {
_cars.value = resource.data!!
}
}
}.launchIn(viewModelScope)
}
}
display
@Composable
fun HomeScreen(viewModel: MainViewModel = hiltViewModel()) {
LazyColumn {
items(viewModel.cars.value) { artWallpaper ->
CardItem(artWallpaper)
}
}
}
@Composable
fun CardItem (artWallpaper: ArtWallpaper) {
Card(
modifier = Modifier
.fillMaxWidth()
.height(650.dp)
.padding(20.dp)
) {
Box(modifier = Modifier.fillMaxSize()) {
AsyncImage(
model = ImageRequest.Builder(LocalContext.current).data(artWallpaper.Link).build(),
modifier = Modifier.fillMaxSize(),
contentDescription = "My content description",
contentScale = ContentScale.FillWidth
)
artWallpaper.Name.let {
Text(
text = it,
fontSize = MaterialTheme.typography.h5.fontSize,
modifier = Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth()
.background(Color.Red),
textAlign = TextAlign.Center,
color = Color.White
)
}
}
}
}
Screen link enter image description here
Database enter image description here