【问题标题】:Flutter Reading firebase颤振阅读火力基地
【发布时间】:2021-03-10 06:19:22
【问题描述】:

我正在关注本教程,以便我可以读取事件的数据 https://petercoding.com/firebase/2020/02/16/using-firebase-queries-in-flutter/ 我去了阅读数据部分“在 ListView 中检索 Firebase 数据”

我将代码粘贴到我的代码中,但在教程中找不到我应该在哪里创建“列表”列表。谁能告诉我应该定义什么列表才能正常工作?

    FutureBuilder(
  
future: databaseReference.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
  
    if (snapshot.hasData) {
    lists.clear();
    Map<dynamic, dynamic> values = snapshot.data.value;
    values.forEach((key, values) {
        lists.add(values);
    });
    return new ListView.builder(
        shrinkWrap: true,
        itemCount: lists.length,
        itemBuilder: (BuildContext context, int index) {
            return Card(
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                Text("Name: " + lists[index]["name"]),
                Text("Age: "+ lists[index]["age"]),
                Text("Type: " +lists[index]["type"]),
                ],
            ),
            );
        });
    }
    return CircularProgressIndicator();
}),

https://gyazo.com/bc6769b8b515919a67030682987d0b85

【问题讨论】:

  • 请将您的代码以文本形式粘贴到此处。并尝试粘贴完整课程的代码
  • 如果你看到这个问题:stackoverflow.com/questions/65035162/… 我正在尝试从 firebase 获取一些项目。我找到了上面的教程,我正在尝试遵循它,但问题是它没有提到他在哪里定义了列表“列表”,我不知道把它放在哪里。我会用代码更新问题。

标签: firebase flutter firebase-realtime-database


【解决方案1】:

您尚未提供小部件类的完整代码,因此我无法为您提供变量的最佳位置。尝试在您的班级中声明名为列表List lists = [] 的变量。我已经在你的构建器中声明了它。它也将在这里工作。但最好让它成为你班级的一个领域。

FutureBuilder(
      
    future: databaseReference.once(),
    builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
      List lists = []; //Just add this line
        if (snapshot.hasData) {
        lists.clear();
        Map<dynamic, dynamic> values = snapshot.data.value;
        values.forEach((key, values) {
            lists.add(values);
        });
        return new ListView.builder(
            shrinkWrap: true,
            itemCount: lists.length,
            itemBuilder: (BuildContext context, int index) {
                return Card(
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                    Text("Name: " + lists[index]["name"]),
                    Text("Age: "+ lists[index]["age"]),
                    Text("Type: " +lists[index]["type"]),
                    ],
                ),
                );
            });
        }
        return CircularProgressIndicator();
    }),

【讨论】:

  • 非常感谢!!我不认为这很容易:))
猜你喜欢
  • 2021-06-05
  • 2021-07-03
  • 2021-03-19
  • 2021-02-03
  • 2020-06-26
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多