警告数据库已锁定为0:00:10.000000。确保在事务期间始终将事务对象用于数据库操作

时间:2020-10-26 14:54:47

标签: android flutter

我正在尝试从远程服务器获取图像。我不断收到此错误:

  Warning database has been locked for 0:00:10.000000. 
  Make sure you always use the transaction object for database operations during a transaction

然后我的应用程序崩溃。


Widget carousel = new Container(
      height: 200,
      child: FutureBuilder<List<String>>(
          future: getPropImgs(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return new Carousel(
                
                boxFit: BoxFit.cover,
                images: snapshot.data.map((f) {
                  return new CachedNetworkImage(
                    imageUrl: f.toString(),
                    imageBuilder: (context, imageProvider) => Container(
                      decoration: BoxDecoration(
                        image: DecorationImage(
                            image: imageProvider,
                            fit: BoxFit.cover,
                            colorFilter:
                                ColorFilter.mode(Colors.blue, BlendMode.dstIn)),
                      ),
                    ),
                    placeholder: (context, url) => Center(
                        child: Container(
                            height: 30,
                            width: 30,
                            child: CircularProgressIndicator())),
                    errorWidget: (context, url, error) =>
                        Image.asset("assets/images/icon.png"),
                  );
                }).toList(),
                autoplay: true,
                animationCurve: Curves.fastOutSlowIn,
                animationDuration: Duration(milliseconds: 1000),
                indicatorBgPadding: 1.0,
                dotColor: Colors.black,
                
              );


1 个答案:

答案 0 :(得分:1)

我猜您正在尝试执行大量的SQL查询。 一种缓慢的解决方案是确保您等待每个电话。在第二行中,您没有等待:

 await db.transaction((txn) async {

但是在这里,它仍然为每个插入创建一个事务,因此将非常慢。一种解决方案是使用批处理(https://github.com/tekartik/sqflite/tree/master/sqflite#batch-support)并将插入语句(例如1000 x 1000)排队;

var batch = db.batch();
batch.rawInsert(statement1);
batch.rawInsert(statement2);
batch.rawInsert(statement3);
...
await db.commit(noResult: true);