Firebase setPersistenceEnabled(true) 对我不起作用,在 dart 中开发应用程序?



我们正在开发一个在dart中的应用程序,我们希望在本地存储数据,以便在应用程序重新启动时,即使没有连接,用户也能看到数据。我已经阅读了有关setPersistenceEnabled属性来执行此操作的信息,但我认为我们做错了什么。请找到以下详细信息 - 有人可以帮助我们吗?

在安卓模拟器Nexus 5上运行应用程序

我们已经将应用程序与安卓版本的Firebase数据库连接起来。

包含在 pubspec.yaml 中的 Firebase_database 和 Firebase_core 包

下面是我们在main.dart中调用setPersistenceEnabled方法的代码 - 正在使用有状态的小部件。

此外,在获取特定集合的数据时,我们使用 REST API。

如果我们做错了什么或需要更多详细信息,请告诉我。

我有connected_products.dart文件,其中我创建了此函数来调用数据库实例。

FirebaseDatabase getDatabaseInstance() {
if (database == null) {
print("should fire once");
database = FirebaseDatabase.instance;
database.setPersistenceEnabled(true);
database.setPersistenceCacheSizeBytes(25000000); //25 MB
}
return database;
}

在下面的同一文件中,我有一个获取产品的函数

Future<Null> fetchProducts() {
_isLoading = true;
notifyListeners();
return getDatabaseInstance()
.reference()
.child("products")
.once()
.then<Null>((DataSnapshot snapshot) {
final List<Product> fetchedProductList = [];
snapshot.value.forEach((dynamic productData) {
final Product product = Product(
blockno: productData['BLOCK_NO'],
name: productData['NAME'],
id: productData['CARD_NO'].toString().isEmpty
? 'Not Available'
: productData['CARD_NO'],
serialno: productData['SR_NO'],
surname: productData['SURNAME'],
firstname: productData['FIRSTNAME'], // This is modified fo search
age: productData['AGE'],
gender: productData['GENDER'],
acnumber: productData['AC_NO'],
isDone: productData['IsDone']);
fetchedProductList.add(product);
});
_products = fetchedProductList;
_isLoading = false;
_isFetchCalled = true;
notifyListeners();
_selProductIndex = null;
}).catchError((error) {
_isLoading = false;
print(error);
notifyListeners();
return;
});
}

我有另一个名为phone.dart的文件 - 我在其中调用此函数

@override
initState() {
if (widget.model.getFetchCalled == false) {
widget.model.fetchProducts();
widget.model.checkToSeeString();
}
widget.model.fetchPhone();
super.initState();
}

因此,当我运行构建时,我在日志中得到的响应低于。 所以不确定数据是否被缓存,因为当我再次关闭和打开应用程序并且我无法看到数据并且不得不通过访问电话小部件进行数据库调用(互联网连接已打开,但数据库调用不会发生,直到我访问 phone.dart 小部件):

我需要从本地存储中显式读取数据吗? 如果是,如何做到这一点

日志详细信息

Background concurrent copying GC freed 920803(35MB) AllocSpace objects, 0(0B) LOS objects, 4% free, 121MB/127MB, paused 1.904ms total 3.032s
W/ample.first_ap( 5637): JNI critical lock held for 16.668ms on Thread[1,tid=5637,Runnable,Thread*=0xe9f5c000,peer=0x756f0760,"main"]
I/ample.first_ap( 5637): Background concurrent copying GC freed 930100(35MB) AllocSpace objects, 0(0B) LOS objects, 4% free, 123MB/129MB, paused 22.580ms total 3.628s
W/ample.first_ap( 5637): JNI critical lock held for 19.275ms on Thread[1,tid=5637,Runnable,Thread*=0xe9f5c000,peer=0x756f0760,"main"]
I/ample.first_ap( 5637): Background concurrent copying GC freed 928850(35MB) AllocSpace objects, 0(0B) LOS objects, 4% free, 130MB/136MB, paused 4.890ms total 2.929s
I/ample.first_ap( 5637): Background concurrent copying GC freed 1427703(38MB) AllocSpace objects, 3(7MB) LOS objects, 3% free, 161MB/167MB, paused 1.943ms total 2.252s
I/ample.first_ap( 5637): Background concurrent copying GC freed 2657600(56MB) AllocSpace objects, 498(23MB) LOS objects, 2% free, 229MB/235MB, paused 1.617ms total 3.073s
I/ample.first_ap( 5637): Background concurrent copying GC freed 2324689(78MB) AllocSpace objects, 1485(69MB) LOS objects, 2% free, 213MB/219MB, paused 1.554ms total 2.347s
I/ample.first_ap( 5637): Waiting for a blocking GC ProfileSaver
I/ample.first_ap( 5637): Background concurrent copying GC freed 2098267(69MB) AllocSpace objects, 1313(61MB) LOS objects, 2% free, 239MB/245MB, paused 1.858ms total 4.004s
I/ample.first_ap( 5637): WaitForGcToComplete blocked ProfileSaver on HeapTrim for 14.705s
I/ample.first_ap( 5637): Waiting for a blocking GC Alloc
I/ample.first_ap( 5637): Background concurrent copying GC freed 5390417(114MB) AllocSpace objects, 12(42MB) LOS objects, 2% free, 226MB/232MB, paused 1.479ms total 2.502s
I/ample.first_ap( 5637): WaitForGcToComplete blocked Alloc on ProfileSaver for 78.480ms
I/ample.first_ap( 5637): Starting a blocking GC Alloc
I/ample.first_ap( 5637): Background concurrent copying GC freed 2346436(77MB) AllocSpace objects, 1451(67MB) LOS objects, 2% free, 218MB/224MB, paused 1.214ms total 1.639s
I/ample.first_ap( 5637): Background concurrent copying GC freed 2119192(71MB) AllocSpace objects, 1400(65MB) LOS objects, 4% free, 129MB/135MB, paused 1.393ms total 2.332s

从注释线程来看,您似乎没有通过启用持久性的客户端检索数据,而是通过 REST API 读取数据。

由于您在 Firebase 客户端上启用了持久性database.setPersistenceEnabled(true),因此只有您通过该客户端读取的数据才会被保留。如果使用其他客户端(甚至不同的 API)来读取数据,则该客户端不会保留该数据。

最新更新