firebase - 类型“_AssertionError”不是“字符串”类型的子类型
问题描述
我已经处理这个问题很长时间了。我正在尝试为每个用户(Uid)解析一组不受欢迎的项目。我得到了响应正文,它被转换为列表。但是我收到了这个错误:
======== Exception caught by widgets library =======================================================
The following _TypeError was thrown building FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic>#7125a):
type '_AssertionError' is not a subtype of type 'String'
The relevant error-causing widget was:
FutureBuilder<dynamic> file:///C:/Users/arunb/AndroidStudioProjects/resplash/lib/pages/bookmark.dart:35:15
When the exception was thrown, this was the stack:
#0 BookmarkPage.build.<anonymous closure> (package:resplash/pages/bookmark.dart:47:44)
#1 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:775:55)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
...
代码:
书签.dart
class BookmarkPage extends StatelessWidget {
const BookmarkPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final sb = context.watch<SignInBloc>();
return RefreshIndicator(
onRefresh: () async {
await context.read<BookmarkBloc>().getData();
},
child: Scaffold(
backgroundColor: Theme.of(context).primaryColor,
appBar: AppBar(
backgroundColor: Theme.of(context).primaryColor,
centerTitle: false,
title: Text('Saved Items'),
),
body: sb.guestUser == true
? EmptyPage(
icon: FontAwesomeIcons.heart,
title: 'No wallpapers found.\n Sign in to access this feature',
)
: FutureBuilder(
future: context.watch<BookmarkBloc>().getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
if (snapshot.data.length == 0)
return EmptyPage(
icon: FontAwesomeIcons.heart,
title: 'No wallpapers found',
);
return _buildList(snapshot);
} else if (snapshot.hasError) {
return Center(
child: Text(snapshot.error),
);
}
return Center(
child: CupertinoActivityIndicator(),
);
},
),
),
);
}
Widget _buildList(snapshot) {
return StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
List d = snapshot.data;
return InkWell(
child: Stack(
children: <Widget>[
Hero(
tag: 'bookmark$index',
child: cachedImage(d[index]['image url'])),
Positioned(
bottom: 15,
left: 12,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
d[index]['category'],
style: TextStyle(color: Colors.white, fontSize: 18),
)
],
),
),
Positioned(
right: 10,
top: 20,
child: Row(
children: [
Icon(Icons.favorite,
color: Colors.white.withOpacity(0.5), size: 25),
Text(
d[index]['loves'].toString(),
style: TextStyle(
color: Colors.white.withOpacity(0.7),
fontSize: 16,
fontWeight: FontWeight.w600),
),
],
),
),
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(
tag: 'bookmark$index',
imageUrl: d[index]['image url'],
catagory: d[index]['category'],
timestamp: d[index]['timestamp'],
)));
},
);
},
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 4 : 3),
mainAxisSpacing: 10,
crossAxisSpacing: 10,
padding: EdgeInsets.all(15),
);
}
}
getData() 方法
final FirebaseFirestore firestore = FirebaseFirestore.instance;
getData() async {
SharedPreferences sp = await SharedPreferences.getInstance();
String _uid = sp.getString('uid');
final DocumentReference ref = firestore.collection('users').doc(_uid);
DocumentSnapshot snap = await ref.get();
List d = snap['loved items'];
List filteredData = [];
if (d.isNotEmpty) {
await firestore
.collection('contents')
.where('timestamp', whereIn: d)
.get()
.then((QuerySnapshot snap) {
filteredData = snap.docs;
});
}
notifyListeners();
return filteredData;
}
我怎么解决这个问题?我不完全知道是什么原因以及如何解决这个问题。
解决方案
不能仅根据此信息确定,但我最好的猜测是
firestore
.collection('contents')
.where('timestamp', whereIn: d)
.get()
正在返回错误。
但是我建议您充分利用 await,因为您正在使用它。还要在 get() 方法上添加一个捕获。所以做类似的事情:
if (d.isNotEmpty) {
try {
const snapshot:QuerySnapshot = await firestore
.collection('contents')
.where('timestamp', whereIn: d)
.get();
(*)
fiteredData = snapshot.docs
}
catch(e) {
console.log(e)
// or/and error handling
}
}
(*)
您还可以尝试在代码 ( (*)
) 中的星号位置记录快照以尝试追踪错误(如果您认为分配有可能导致问题)
我不确定这是否能解决您的问题(如果没有可重现的示例,很难确切知道出了什么问题),但它至少应该可以帮助您找出问题所在。
推荐阅读
- reactjs - firebase.messaging().setBackgroundMessageHandler(*) 'handler' 需要一个函数
- c - 无法为 Windows 编译 PuTTY:致命错误 RC1015:无法打开包含文件“winresrc.h”
- javascript - 如何显示文件名而不是目录?
- google-sheets - Google sheet QUERY + MATCHES 函数不排除字符串
- google-sheets - 将列中的重复行合并为逗号分隔值 - Google Query
- wordpress - 未收到 Wordpress 授权标头
- c - 如何修复此程序中的二进制文件 a 和 b 不同的错误?
- vb.net - Crystal Report“已达到系统管理员配置的最大报表处理作业限制”
- python - Python将两个文件合并在一起
- java - 如何使用 rosjava 获取所有正在运行的 ros 节点的列表?