我今天才开始使用firebase。我正在尝试将firebase中的虚拟数据传递给一个列表。使用快照,我将数据提取到一个名为fetched data的变量中,并将我的数据传递给列表。但随后,我将快照转换为DataContent。我得到的指定类型转换无效。为什么我的投射不能工作,Firebase有一个独特的投射方式?
下面是我的代码的一小部分
List<DataContent>DisplayData = new List <DataContent>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.myView);
firebase = new FirebaseClient(FirebaseUrl);
mAuth = FirebaseAuth.Instance;
mLike = FirebaseDatabase.Instance.Reference.Child("Table");
mDatabase.AddValueEventListener(this);
mDatabase.KeepSynced(true);
public void OnCancelled(DatabaseError error)
{
throw new NotImplementedException();
}
public void OnDataChange(DataSnapshot snapshot)
{
var fireBaseData = snapshot.Child(postBlogKeyId);
DisplayData.Add((DataContent)fireBaseData);
}发布于 2017-06-28 10:45:28
我得到的指定类型转换无效。为什么我的投射不能工作,Firebase有一个独特的投射方式?
首先,请确保它是一个对象,而不是snapshot.Child(postBlogKeyId)下的列表。
其次,通过snapshot.Child(postBlogKeyId),您将获得一个DataSnapShot对象,它是数据内容的包装器,您需要使用snapshot.Child(postBlogKeyId).Value来获取数据内容。
如果仍然不能工作,请调试snapshot.ToString()并发布结果。
https://stackoverflow.com/questions/44773739
复制相似问题