Skip to content

1.2.8

Latest

Choose a tag to compare

@Knightwood Knightwood released this 19 Feb 02:36
· 2 commits to master since this release

增加datastore委托工具。

这是一个示例:

//1. 需要有一个协程作用域
val scope = CoroutineScope(Dispatchers.IO)
//2. 你可以将属性委托给datastore,变量名就是key的名称
var username by dataStore.getting(11, scope)

//3. 对数据读写就可以存储到datastore
MaterialTheme {
    //可以在compose中观察数据变化
    val va = dataStore.asDataFlow<Int>("username").collectAsState(initial = 11)
    Column {
        Button(onClick = {
            val randoms = Random.nextInt(0, 11)
            //赋值就会将数据写入datastore
            username = randoms
            //访问变量就可以得到刚刚写入的数据
            println(username)
        }) {
            Text("Random")
        }
        Text("value:${va.value}")
    }
}