Why is information not being retrieved from the Room Database when returned as a Flow?

Everything was working fine initially, but later I added a table to the database. I have wiped all data and started fresh, so I don’t think its a migration issue.

The initial tables are working fine as expected. For the new table when I run the query through app inspector then it works fine within the app inspector. However when the query (specified in the dao) is called through the ViewModel, it returns an empty list.

within the DAO:

@Query("SELECT * FROM PropTable")
fun readPropReport(): Flow<List<PropReport>>

within the viewModel:

val _propReportsU = dao.readPropReport().stateIn(viewModelScope, SharingStarted.WhileSubscribed(500),
emptyList())
Log.i("Prop Function", "Prop Reports 'U': ${_propReportsU.value}")

The Log shows that the returned result is an empty list, but as I said the table is not empty when running the query through the app inspector it is not empty.

When the query is run I expected to see the values in the table, however it results in an empty set. Running the query through the app inspector returns the expected values from the table.

Room tutorial I followed: https://www.youtube.com/watch?v=bOd3wO0uFr8&ab_channel=PhilippLackner

New contributor

Siddhant Samarth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

readPropReport returns a Flow that you convert to a StateFlow by calling stateIn.

A StateFlow is an asynchronous datatype that represents a value that can change over time. It is initialized with an emptyList() until the database returns the result from the query.

You log its value directly after it was created, before the database had enough time to execute the query, so you get an empty list (the initial value). If you would log the value again after some time, it would contain the expected results.

But Flows are usually meant to be collected like this:

_propReportsU.collect {
    Log.i("Prop Function", "Prop Reports 'U': $it")
}

Now, everytime the value of _propReportsU changes, a log message is printed. Should be twice for you: First an empty list, then the results from your query. Note that collect is a suspend function, so it must be called from a coroutine or another suspend function.


The common pattern for flows in the view model is different from what you do, though. Usually you want to expose the flow as a property for the UI can collect:

class MyViewModel(
    dao: MyDao,
) : ViewModel() {
    val propReportsU = dao.readPropReport()
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5_000),
            initialValue = emptyList(),
        )
}

When you use Compose for your UI, the collection of the flow would look like this:

@Composable
fun MyComposable(myViewModel: MyViewModel = viewModel()) {
    val propReports: List<PropReport> by myViewModel.propReportsU.collectAsStateWithLifecycle()
}

propReports will now always contain the most current state of the list: Empty at first, then, after the query finished, the query’s result.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật