gpt4 book ai didi

firebase - 显示从Firestore中检索到的数组字段时出错

转载 作者:行者123 更新时间:2023-12-03 04:38:17 24 4
gpt4 key购买 nike

我正在尝试使用streambuilder检索数组“类”,如下图所示。我使用future builder尝试了同样的方法,但是从前面提出的问题中找不到合适的解决方案。如何检索类(class)列表并在ListView中显示?
当我将鼠标悬停在['fields']上时,错误在snapshot.data['fields']['class'][position]上显示:

The argument type 'String' can't be assigned to the parameter type 'int'


enter image description here
enter image description here
我使用streambuilder实现了以下代码:
var ref;
List values = [];
Future<void> getassignment() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final name = prefs.getString('orgname') ?? '';
print(name);
var ref = FirebaseFirestore.instance.collection('Org').doc(name).collection('Login').doc(FirebaseAuth.instance.currentUser.uid);
print(ref);
ref.snapshots().forEach((doc) {

values = List.from(doc.data()['fields']['class']);
print(values);


});
}
@override
void initState() {

getassignment();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF1976D2),
body: StreamBuilder<List<DocumentSnapshot>>(
stream: ref.snapshots(),
builder: (context,snapshot){
if(!snapshot.hasData){
print("Error");
}else{
return Container(
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, position) {
return GestureDetector(
onTap: (){
Navigator.of(context).push(MaterialPageRoute<Null>(
builder: (BuildContext context){
return new SubjectList(
clas:snapshot.data['fields']['class'][position].toString(),
);
}
));
},
child: Card(

child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(snapshot.data['fields']['class'][position].toString(), style: TextStyle(fontSize: 22.0),),

),
),
);
},
),
);

}
},
)


);
}

最佳答案

实际上,您的类数组中的数据是字符串,而不是整数。您可以知道,因为它的值前后都有引号。如果它是数字类型的值,则将不带引号。
如果在那里需要数字,则写数据的代码应使用数字类型变量,而不是字符串。您可能需要convert it to a number first
如果您不能更改编写文档的代码,则必须更改读取数据库的代码才能将字符串转换为数字。

关于firebase - 显示从Firestore中检索到的数组字段时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63724236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com