Firestore子集合返回空无报错,Android+Hilt环境求排查指导
问题排查:Android应用通过Hilt+Firestore获取用户子集合数据失败
问题背景
我正在开发一款Android应用,目标是获取特定用户Firestore中plants子集合的所有植物数据。Firestore数据结构为:users集合 -> 单个用户文档 -> plants子集合,每个植物文档包含id、plant_category_id、image、name等字段。
现有代码
仓库实现类(PlantRepositoryImpl)
@Singleton class PlantRepositoryImpl @Inject constructor( private val userRef: CollectionReference ): PlantRepository { override fun getPlantsFromFirestore() = callbackFlow { val user = Firebase.auth.currentUser!!.uid // Log.d("Data:","${user}") val snapshotListener = userRef.document(user).collection("plants").orderBy("name").addSnapshotListener{ snapshot, e -> val response = if (snapshot != null) { val plant = snapshot.toObjects(Plant::class.java) Success(plant) } else { Error(e?.message ?: e.toString()) } trySend(response).isSuccess } awaitClose { snapshotListener.remove() } } }
Hilt依赖注入模块(AppModule)
@Module @InstallIn(SingletonComponent::class) object AppModule { @Provides fun provideFirebaseFirestore()= Firebase.firestore @Provides fun provideUserRef( db: FirebaseFirestore )=db.collection("users") @Provides fun provideUserRepository( userRef: CollectionReference ): PlantRepository = PlantRepositoryImpl(userRef) @Provides fun provideUseCases( repo: PlantRepository ) = UseCases( getPlants = GetPlants(repo), addPlant = AddPlant(repo), deletePlant = DeletePlant(repo), editPlant= EditPlant(repo) ) }
Plant数据类
data class Plant( val id:String?=null, val plant_category_id:String?=null, val image:String?=null, val name:String?=null, val category:String?=null, val room_location:String?=null, )
MainActivity
@AndroidEntryPoint class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { IndoorPlantCareTheme { val navController = rememberNavController() SetupNavGraph(navController = navController) } } } }
自定义Application类(IndoorPlantCare)
@HiltAndroidApp class IndoorPlantCare: Application()
Manifest配置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.indoorplantcare"> <uses-permission android:name="android.permission.INTERNET"/> <application android:name=".IndoorPlantCare" android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.IndoorPlantCare" tools:targetApi="31" tools:ignore="MissingClass"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.IndoorPlantCare"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
UI代码
when (plantResponse) { is Loading -> ProgressBar() is Success ->LazyColumn( modifier = Modifier .fillMaxSize() .background(Color.White) ) { items(items = plantResponse.data) { CardList(model = it) { model -> Log.d("TAG", "Clicked on $model") // navController.navigate(route = NavScreens.DetailScreen.name + "/$model") } } } is Error -> printError(plantResponse.message) } } } @Composable fun CardList(model: Plant, onItemClick:(String) -> Unit = {} ){ Card( shape = MaterialTheme.shapes.small, modifier = Modifier .padding( bottom = 6.dp, top = 6.dp, start = 6.dp, end = 6.dp, ) .clickable { onItemClick(model.id.toString()) } .fillMaxWidth(), elevation = 8.dp ) { Column { Image( painter = rememberAsyncImagePainter(model = model.image) , contentDescription = null, modifier = Modifier .fillMaxWidth() .height(125.dp), contentScale = ContentScale.Crop ) Row( modifier = Modifier .fillMaxWidth() .padding( top = 12.dp, bottom = 12.dp, start = 8.dp, end = 8.dp ) ) { Text( text = model.name.toString(), modifier = Modifier .fillMaxWidth(0.85f) .wrapContentWidth(Alignment.Start), style = MaterialTheme.typography.h5 ) } Text( text = "Author : ${model.room_location}", modifier = Modifier .fillMaxWidth(0.85f) .wrapContentWidth(Alignment.Start) .padding( top = 12.dp, bottom = 12.dp, start = 8.dp, end = 8.dp ), style = MaterialTheme.typography.h6 ) } } }
问题现象
调整代码后出现不熟悉的Hilt相关错误,除此之外无其他报错,甚至Log.d("Data:","${user}")也没有输出调试信息,怀疑问题出在AppModule配置上,需要排查指导。
内容的提问来源于stack exchange,提问作者MayWheather
相关产品推荐
相关产品推荐

