我们正在使用dart开发一个应用,该应用中我们希望在本地存储数据,以便在应用重新启动时,即使没有连接,用户也可以看到数据。我已经读过setPersistenceEnabled属性,但是我认为我们做错了。请找到以下详细信息-有人可以帮助我们。
在android模拟器Nexus 5上运行应用
我们已将应用程序与Firebase数据库连接为Android版本。
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();
}
因此,当我运行build时,我得到的响应低于日志记录。所以不知道数据是否被缓存,因为当我再次关闭并打开应用程序时,我无法看到数据并且必须通过访问电话小部件进行数据库调用(互联网连接已打开,但是直到我之前数据库调用才会发生访问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
答案 0 :(得分:0)
从注释线程看来,您并不是通过启用了持久性的客户端来检索数据,而是通过REST API读取数据。
由于您使用database.setPersistenceEnabled(true)
在Firebase客户端上启用了持久性,因此只有通过该客户端读取的数据将被持久化。如果您使用其他客户端(甚至是不同的API)读取数据,则该客户端将不会保留该数据。