【发布时间】:2020-08-13 13:08:13
【问题描述】:
我现在尝试了几天来检索我的 firestore 值,但没有运气所以把它贴在这里。
我有一个 Firestore 数据库和一些数据。我想在 Flutter 的帮助下检索它。
这就是我一直在做的。
所以我有一个 Flutter 屏幕,它在 AppBar 中显示一个简单的 3 点下拉菜单。
它有两个选项:edit 和 cancel。
我想要的是,当我按下编辑时,它应该会打开一个新屏幕并传递我从 firestore 检索到的数据。
这是我有 edit 和 cancel 下拉菜单(3 个点)并调用 a 函数(检索数据并打开新屏幕)的地方。
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(widget.news.headline.toUpperCase()),
actions: <Widget>[
PopupMenuButton<String>(
onSelected: (value) {
_open_edit_or_delete(value); // caling the function here
},
itemBuilder: (BuildContext context) {
return {'Edit', 'Delete'}.map((String choice) {
return PopupMenuItem<String>(
value: choice,
child: Text(choice),
);
}).toList();
},
),
],
),
body: _get_particular_news(widget.news),
);
}
这是它正在调用的open_edit_or_delete 函数。但它不会打开(导航)到我正在调用的屏幕。
open_edit_or_delete(String selectedOption) {
News news;
Visibility(
visible: false,
child: StreamBuilder(
stream: FireStoreServiceApi().getNews(),
builder: (BuildContext context, AsyncSnapshot<List<News>> snapshot) {
if (snapshot.hasError || !snapshot.hasData) {
Navigator.push(
context, MaterialPageRoute(builder: (_) => FirstScreen(news:news)));
return null;
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
news = snapshot.data[index];
},
);
}
},
));
}
如果您需要FireStoreServiceApi().getNews(),这里也有。
// get the news
Stream<List<News>> getNews() {
return _db.collection("news").snapshots().map(
(snapshot) => snapshot.documents
.map((doc) => News.fromMap(doc.data, doc.documentID))
.toList(),
) ;
}
有人可以帮帮我吗?
【问题讨论】:
-
你能补充一下现在发生的事情吗?我认为您总是被重定向到 FirstScreen ?我说的对吗?
-
什么也没发生。屏幕保持不变。这是一个小部件,我想这就是问题所在。我使用了小部件,因为我不知道如何在没有
StreamBuilder小部件的情况下将数据检索为News对象。你能帮帮我吗?
标签: flutter google-cloud-firestore