首页 > 解决方案 > 文本小部件将实时 Fireabase 值检测为空

问题描述

我想从我的实时数据库中获取这些值并将它们放入我的Text小部件中我已经在我的应用程序中成功检索到它们唯一的问题是使它们在我的Text小部件中可用

在此处输入图像描述

这些是我想String从实时数据库中获取的文本小部件

第一个文本小部件

Text(
    getSnapShotData('orders_count'),//here is the function being run
    style: TextStyle(
      color: Colors.black,
      fontSize: 16,
      fontFamily: "Sofia_Pro",
      fontWeight: FontWeight.w300,
    ),
  ),

第二个文本小部件

Text(
    getSnapShotData('name'),//here is the function being run
    style: TextStyle(
      color: Colors.black,
      fontSize: 16,
      fontFamily: "Sofia_Pro",
      fontWeight: FontWeight.w300,
    ),
  ),

从实时数据库中获取的功能

String getSnapShotData(String child){
 databaseReference.child("users").child(_firebaseUser.uid).child(child).once().then((DataSnapshot snapshot) {
  var foo = snapshot.value;
  var fooString = snapshot.value.toString();
  
  print(snapshot.value);
  print("type is ${foo.runtimeType}");
  print("type is ${fooString.runtimeType}");
  
  return fooString;
  });
}

有趣的是,如果我记录获取的值,我会得到正常的结果,唯一的问题是当我将获取的数据用于Text小部件时

这是获取数据的日志结果

  I/flutter (29458): 4
  I/flutter (29458): type is int
  I/flutter (29458): type is String

标签: firebaseflutterdartfirebase-realtime-database

解决方案


推荐阅读