【发布时间】:2026-02-21 04:50:01
【问题描述】:
{
"story" : {
"-MTOpnFnINch-hCT4tG7" : {
"creatorId" : "KEieXFzt9zezgP9684EhCxRNzp42",
"img" : "https://www.petyourdog.com/uploads/breed_lists/Toy-Dog-Breeds.jpg",
"story" : "111111111111333333333333333333333333333333333333333333333333222222222",
"title" : "111111111111111111"
},
"-MTOq-_X96CLJ6-v6WFx" : {
"creatorId" : "EJW4BU1IRMUtchW1bzUPmeLzttZ2",
"img" : "https://naturaldogcompany.com/wp-content/uploads/2016/03/shutterstock_194843813-web-180x180.jpg",
"story" : "qqqqqqqqqqqqwwwwwwwwwwwwwwwwwqwwwwww",
"title" : "qqqqqqqq"
},
"-MTP0N2MkCj44I7aPudf" : {
"creatorId" : "KEieXFzt9zezgP9684EhCxRNzp42",
"img" : "https://naturaldogcompany.com/wp-content/uploads/2016/03/shutterstock_194843813-web-180x180.jpg",
"story" : "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk",
"title" : "yyyyykkkkkkkkkkkkkkkk"
},
"-MTUgGBHkcnOBCMmToGU" : {
"creatorId" : "KEieXFzt9zezgP9684EhCxRNzp42",
"img" : " ",
"story" : "qqqqqqqqqqqqqqqqqqqqqqqwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqq",
"title" : "ooooooooooooo"
}
},
"userFavorites" : {
"KEieXFzt9zezgP9684EhCxRNzp42" : {
"-MTOq-_X96CLJ6-v6WFx" : true
}
}
}
这是我正在处理的数据。我试图过滤用户故事页面上的数据,其中仅显示用户自己的数据。
代码如下:
class Stories with ChangeNotifier {
List<Story> _storys = [
];
final String authToken;
final String userId;
Stories(this.authToken, this.userId, this._storys);
List<Story> get storys {
return [..._storys];
}
List<Story> get favoriteItems {
return _storys.where((storyItem) => storyItem.isFavorite).toList();
}
Story findById(String id) {
return _storys.firstWhere((story) => story.id == id);
}
Future<void> fetchAndSetProducts([bool filterByUser = false]) async {
final filterString = filterByUser ? 'orderBy="creatorId"&equalTo="$userId"' : '';
var url =
'https://unified-adviser--#####..firebaseio.com/story.json?auth=$authToken&$filterString';
try {
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
if (extractedData == null) {
return;
}
url =
'https://unified-adviser-#####.firebaseio.com/userFavorites/$userId.json?auth=$authToken';
final favoriteResponse = await http.get(url);
final favoriteData = json.decode(favoriteResponse.body);
final List<Story> loadedProducts = [];
extractedData.forEach((prodId, prodData) {
loadedProducts.add(Story(
id: prodId,
title: prodData['title'],
story: prodData['story'],
// price: prodData['price'],
isFavorite:
favoriteData == null ? false : favoriteData[prodId] ?? false,
img: prodData['img'],
));
});
_storys = loadedProducts;
notifyListeners();
} catch (error) {
throw (error);
}
}
.
.
.
.
我正在尝试过滤数据的用户故事页面
class UserStory extends StatelessWidget {
static const routeName = 'userStory';
Future<void> _refreshProducts(BuildContext context) async {
await Provider.of<Stories>(context, listen: false)
.fetchAndSetProducts(true);
}
@override
Widget build(BuildContext context) {
print('rebuilding...');
return Scaffold(
appBar: AppBar(
title: const Text('Your Products'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
Navigator.of(context).pushNamed(EditStoryScreen.routeName);
},
),
],
),
// drawer: AppDrawer(),
body: FutureBuilder(
future: _refreshProducts(context),
builder: (ctx, snapshot) =>
snapshot.connectionState == ConnectionState.waiting
? Center(
child: CircularProgressIndicator(),
)
: RefreshIndicator(
onRefresh: () => _refreshProducts(context),
child: Consumer<Stories>(
builder: (ctx, productsData, _) => Padding(
padding: EdgeInsets.all(8),
child: ListView.builder(
itemCount: productsData.storys.length,
itemBuilder: (_, i) => Column(
children: [
UserStoryItem(
productsData.storys[i].id,
productsData.storys[i].title,
productsData.storys[i].img,
),
Divider(),
],
),
),
),
),
),
),
);
}
}
我使用的方法不起作用,即使我使用新帐户注册,我也可以查看 UserStory 页面中的所有数据并编辑或删除它们!
这里有什么问题?
【问题讨论】:
-
看起来您正在使用某种 Firebase 功能来访问实时数据库。您需要检查您正在调用的此函数并了解发生了什么。还要查看函数日志以查看是否有错误。对我来说,过滤器字符串看起来也很奇怪,因为 &equalTo="$userId"' 没有说明数据库的哪个字段应该等于 userId,它看起来应该是 creatorId...
-
很难说你共享的代码量出了什么问题。你能打印
url并告诉我们它显示了什么价值吗? -
您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 your Firebase Database console 的溢出菜单 (⠇) 中的 Export JSON 链接轻松获得。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,总的来说这只是一件好事。
-
@FrankvanPuffelen 完成。为了更具体,您要打印哪个 var?我将研究 fetchAndSetProducts 中的提取数据
-
url的值设置在这一行之后var url = 'https://unified-adviser--#####..firebaseio.com/story.json?auth=$authToken&$filterString';
标签: flutter firebase-realtime-database