Android 如何在jetpack compose中传递来自Firestore的查询参数
我的项目中有一个查询,用于检索游戏列表并将其显示在lazyColumn中。当我查询整个集合时,它可以正常工作,但是我想放一个where子句,它包含2个参数,我必须在片段中传递这些参数,我不知道如何使用Jetpack Compose传递它们 以下是已经存在并正在运行的查询:Android 如何在jetpack compose中传递来自Firestore的查询参数,android,firebase,kotlin,google-cloud-firestore,android-jetpack-compose,Android,Firebase,Kotlin,Google Cloud Firestore,Android Jetpack Compose,我的项目中有一个查询,用于检索游戏列表并将其显示在lazyColumn中。当我查询整个集合时,它可以正常工作,但是我想放一个where子句,它包含2个参数,我必须在片段中传递这些参数,我不知道如何使用Jetpack Compose传递它们 以下是已经存在并正在运行的查询: @Module @InstallIn(SingletonComponent::class) object AppModule { @Provides @Named("queryGame")
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Named("queryGame")
fun provideGetAllGames(): Query =
FirebaseFirestore.getInstance()
.collection(GAMES)
}
我需要在appModule中添加.where(PLAYERS,listOf(user1+“”+user2,user2+“”+user1)),但我不知道如何在片段上传递user1和user2。我怎样才能做到这一点
我需要在appModule中添加.where(PLAYERS,listOf(user1+“”+user2,user2+“”+user1))
无法将动态数据传递到AppModule文件中。因此,要使用查询对象从Firestore获取数据,您需要在repository类中添加.where()
调用。看到您已经将“QueryName”对象(实际上是一个CollectionReference对象)注入到您的repository类中,您可以使用以下代码行:
dataOrException.data = queryGame.whereIn(PLAYERS, listOf(user1 + "" + user2, user2 + "" + user1)).get()
不要忘记在使用此查询的方法中同时传递“user1”和“user2”对象。但是在调用viewModel上的函数时如何传递参数?该函数在viewModel的Init{}块中被调用,它会更新数据可变状态。如果您在活动类中获得“user1”和“user2”对象,那么您应该将它们作为参数传递到viewModel类中,然后按术语传递到repository类中。嗨,Henrique!一切都好吗?我能帮你提供其他信息吗?是的,我想我现在一切都准备好了。谢谢你很高兴听到这个消息,亨里克;)
@HiltViewModel
class GamesViewModel @Inject constructor(
private val repository: PlayersRepository
): ViewModel() {
val data: MutableState<DataOrException<List<Game>, Exception>> = mutableStateOf(
DataOrException(
listOf(),
Exception("")
)
)
init{
getAllGames()
}
fun getAllGames() {
viewModelScope.launch {
data.value = repository.getAllGames()
}
}
@AndroidEntryPoint
class GameFragment : Fragment() {
private val viewModel: GamesViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
val games = viewModel.data.value.data
LazyColumn{
items(games!!){ game ->
GameResults(game)
}
}
dataOrException.data = queryGame.whereIn(PLAYERS, listOf(user1 + "" + user2, user2 + "" + user1)).get()