小编Nic*_*sco的帖子

单元测试室和LiveData

我目前正在使用新的Android架构组件开发应用程序.具体来说,我正在实现一个房间数据库,LiveData它在其中一个查询上返回一个对象.插入和查询按预期工作,但是我在使用单元测试测试查询方法时遇到问题.

这是我试图测试的DAO:

NotificationDao.kt

@Dao
interface NotificationDao {

@Insert
fun insertNotifications(vararg notifications: Notification): List<Long>

@Query("SELECT * FROM notifications")
fun getNotifications(): LiveData<List<Notification>>
Run Code Online (Sandbox Code Playgroud)

}

正如你所知,查询函数返回一个LiveData对象,如果我将其更改为a List,Cursor或者基本上是什么,然后我得到预期结果,即插入数据库中的数据.

问题是,下面的测试将始终失败,因为value该的LiveData对象始终是null:

NotificationDaoTest.kt

lateinit var db: SosafeDatabase
lateinit var notificationDao: NotificationDao

@Before
fun setUp() {
    val context = InstrumentationRegistry.getTargetContext()
    db = Room.inMemoryDatabaseBuilder(context, SosafeDatabase::class.java).build()
    notificationDao = db.notificationDao()
}

@After
@Throws(IOException::class)
fun tearDown() {
    db.close()
}

@Test
fun getNotifications_IfNotificationsInserted_ReturnsAListOfNotifications() {
    val NUMBER_OF_NOTIFICATIONS = 5
    val …
Run Code Online (Sandbox Code Playgroud)

android unit-testing kotlin android-room android-architecture-components

44
推荐指数
5
解决办法
1万
查看次数

如何在Flutter中使用不同色调的色样?

我正在尝试使用颤振来学习应用程序开发.在默认的flutter应用程序代码中,我尝试将以下代码更改为

primarySwatch: Colors.blueGrey

primarySwatch: Colors.blueGrey[500]
Run Code Online (Sandbox Code Playgroud)

但这会引发错误 手机上的消息

 ??? EXCEPTION CAUGHT BY WIDGETS LIBRARY ????????????????????????????????????????????????????????????
I/flutter ( 4512): The following assertion was thrown building MyApp(dirty):
I/flutter ( 4512): type 'Color' is not a subtype of type 'MaterialColor' of 'primarySwatch' where
I/flutter ( 4512):   Color is from dart:ui
I/flutter ( 4512):   MaterialColor is from package:flutter/src/material/colors.dart
I/flutter ( 4512):   int is from dart:core
I/flutter ( 4512): 
I/flutter ( 4512): Either the assertion indicates an error in the framework itself, or we should provide substantially …
Run Code Online (Sandbox Code Playgroud)

android dart material-design flutter

9
推荐指数
2
解决办法
7077
查看次数

使用缓存引擎启动 FlutterActivity 子类

我目前正在将使用 Flutter 开发的视图添加到现有的 Android 应用程序中。我一直在关注 Flutter网站上的教程,并决定使用缓存引擎,以最大限度地减少用户在导航到应用程序的 Flutter 部分时可能遇到的延迟。为此,您必须启动 Flutter 活动,例如

startActivity(
    FlutterActivity
      .withCachedEngine("my_engine_id")
      .build(this) // this is a Context
Run Code Online (Sandbox Code Playgroud)

)

一段时间后,我需要编写一个方法通道以从应用程序的 Flutter 部分通信回 Android 主机应用程序,因此我按照 Flutter 的另一个教程中的说明进行操作,其中显示实现通道的活动必须延伸FlutterActivity

所以我的问题是我不确定如何使用缓存引擎初始化这个活动,因为我显然不能再使用FlutterActivity.withCachedEngine了。有没有人已经解决了这个问题?

android kotlin flutter

5
推荐指数
1
解决办法
568
查看次数

Flutter 中基于百分比的布局

来自 Android 背景,我习惯于ConstraintLayout为我的 UI 定义基于百分比的边距和大小。我想知道 Flutter 中是否有一个小部件可以实现类似的功能。我一直在查看小部件文档,但我还没有找到任何东西。

flutter flutter-layout

0
推荐指数
1
解决办法
5180
查看次数