简体   繁体   English

如何从flutter中的firebase数据填充列表视图

[英]How to populate listview from firebase data in flutter

Hi i am trying to get data from firebase realtime database and populate listview in flutter , following is image please check before proceeding,嗨,我正在尝试从 firebase 实时数据库获取数据并在 flutter 中填充列表视图,以下是图像,请在继续之前检查,

Here is the image from firebase Now i want to fetch data and populate listview in flutter app, currently i'm getting in terminal like this :这是来自firebase的图像现在我想在flutter应用程序中获取数据并填充列表视图,目前我正在进入这样的终端:

Data : {Reported By shivam kapasia: {Reporter Email: kapasiashivam007@gmail.com, Description: need help, Reported by: shivam kapasia, latitude: 28.4725024, Time: 5:46 PM, location: null, Date: Mon 20 Jan, longitude: 77.506273},数据:{Shivam kapasia 报道:{Reporter Email:kapasiashivam007@gmail.com,描述:需要帮助,报道人:shivam kapasia,纬度:28.4725024,时间:下午 5:46,地点:空,日期:1 月 20 日星期一,经度:77.506273},

Here is my code:这是我的代码:

final databaseReference = FirebaseDatabase.instance.reference();
void createRecord() {
  _getCurrentLocation();
  databaseReference.child('Reported By ' + user.displayName).set({
    'Reported by': user.displayName,
    'Reporter Email': user.email,
    'Date': formattedDate,
    'Time': res,
    'Description': '$message',
    'longitude': longitude.toString(),
    'latitude': latitude.toString(),
    'location': '$_currentAddress'
  });
}

void getData() {
  databaseReference.once().then((DataSnapshot snapshot) {
    print('Data : ${snapshot.value}');
  });
}

Both function are working fine.两个功能都运行良好。 please help me if you know how to build listview according to data from firebase and populate them accordingly by using upper function, please try to give solution in code that will be more helpful.如果您知道如何根据来自 firebase 的数据构建列表视图并使用上层函数相应地填充它们,请帮助我,请尝试在代码中提供更有用的解决方案。

Thanks in advance...提前致谢...

You need to create a class for all this data, your createRecord() function isn't very efficient.您需要为所有这些数据创建一个类,您的createRecord()函数效率不高。
Consider creating a class Report , save all the information into an object of Report .考虑创建一个Report类,将所有信息保存到一个Report对象中。
Then, create a function within Report called toJSON() which will convert all your data to JSON format and you can directly pass it to firebase.然后,在Report创建一个名为toJSON()的函数,它将您的所有数据转换为 JSON 格式,您可以直接将其传递给 firebase。
Similarly, you can create a named constructor called Report.fromJSON(Map snapshot) which will give you an object of Report from the snapshot you got from the database.同样,您可以创建一个名为Report.fromJSON(Map snapshot)的命名构造函数,它将根据您从数据库获得的快照为您提供一个Report对象。
Now, you can create a list List<Report> reportList;现在,您可以创建一个列表List<Report> reportList; containing all the reports you have fetched from firebase.包含您从 firebase 获取的所有报告。
Now all you have to do is map it into a ListView as follows,现在你要做的就是将它映射到一个ListView ,如下所示,

ListView(
children: reportList.map((element){return Container();}).toList(),
)


In the above example element will be an object of Report and you'll be able to display the data inside a Widget for example, a Card or a Container在上面的示例中, element将是Report一个对象,您将能够在Widget显示数据,例如CardContainer


If you're only looking to display one instance of Record you can manually do it by adding Widgets into the ListView ,如果您只想显示Record一个实例,您可以通过将 Widgets 添加到ListView来手动完成,

ListView( children: <Widget>[] )


If there's anything specific you need help with like setting up the class and the functions leave a comment, I'd be glad to help.如果您有任何具体需要帮助,例如设置课程和功能,请发表评论,我很乐意提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM