【发布时间】:2021-05-24 12:51:12
【问题描述】:
我在从 Firestore 读取数组时遇到问题,字符串可以在未来的构建器中读取,如下所示,
然而,当读取同一文档中的数组字段并插入列表的时间不起作用时,实际上我无法使用 get 方法读取数组。尝试了 [] 方法,但总是收到 null 错误,List experience = snapshot.data.get('experiences') 不适用于数组,
从 firebase 读取数组有不同的方法吗?我还需要读取放入列表中的数组,并使用它将每个索引中的选定数据列出到列表视图,就像我们通常在本地使用列表所做的那样。
这是错误信息:
======== 小部件库捕获的异常=================================== ====================== 在构建 FutureBuilder(dirty, state: _FutureBuilderState#395a0) 时引发了以下 NoSuchMethodError: 在 null 上调用了方法“get”。 接收方:空 尝试调用:get("experiences")
相关的导致错误的小部件是: FutureBuilder file:///xxxxxxxx/lib/regformSummary.dart:185:45 抛出异常时,这是堆栈: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 _RegFormSummaryState.build。 (包:xxxxxx/regformSummary.dart:186:94) #2 _FutureBuilderState.build(包:flutter/src/widgets/async.dart:773:55) #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27) #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15) ...
这是读取字符串的工作代码
//under main widget builder
DocumentReference resumedata =_firestore.collection('users').doc(widget.userID).collection('resume').doc('info');
DocumentReference userdata = _firestore.collection('users').doc(widget.userID).collection('resume').doc('personalinfo');
//
.....,
FutureBuilder(future:userdata.get(),builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if(snapshot.connectionState == ConnectionState.done){
String profilepic = snapshot.data.get('profilepic');
String name = snapshot.data.get('name');
String sname = snapshot.data.get('sname');
return Column(
children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 10),
child: CircleAvatar(
minRadius: 40,
maxRadius: 80,
backgroundImage: NetworkImage('$profilepic'),
),
),//avatar
Padding(
padding: EdgeInsets.symmetric(horizontal: 10,vertical:3),
child: Text(
name,
style: GoogleFonts.openSans(textStyle: TextStyle(color: Colors.black54,fontSize: 24,fontWeight: FontWeight.bold)),
),
),//name
Padding(
padding: EdgeInsets.symmetric(horizontal: 10,vertical:3),
child: Text(
sname,
style: GoogleFonts.openSans(textStyle: TextStyle(color: Colors.black54,fontSize: 24,fontWeight: FontWeight.bold)),
),
),//sname
],
);
}
return SizedBox();
}),
这是失败的代码
DefaultTabController(
length:8,
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Column(
children: [
TabBar(
isScrollable: true,
indicatorWeight: 4.0,
indicatorColor:Colors.black38,
unselectedLabelColor: Colors.black38,
tabs: <Widget>[
Tab(
child:Text('Tecrübeler',style: TextStyle( color:
colorVal
)),
),
Tab(
child: Text('Eğitim',style: TextStyle( color: colorVal
)),
),
Tab(
child: Text('Yabancı Dil',style: TextStyle( color: colorVal)),
),
Tab(
child: Text('Sertifikalar',style: TextStyle( color: colorVal)),
),
Tab(
child: Text('Referanslar',style: TextStyle( color:colorVal)),
),
Tab(
child: Text('İlgilendiği Pozisyonlar',style: TextStyle( color: colorVal)),
),
Tab(
child: Text('CV dosyası ve Önyazı Dosyası',style: TextStyle( color: colorVal)),
),
Tab(
child: Text('İletişim Bilgileri',style: TextStyle( color: colorVal)),
),
],
),
SizedBox(
height:MediaQuery.of(context).size.height,
child: TabBarView(
children: <Widget>[
FutureBuilder(future:resumedata.get(),builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
List experiences = List.from(snapshot.data.get('experiences'));
if (snapshot.connectionState == ConnectionState.done){
ListView.builder(
itemCount: experiences.length,
itemBuilder: (context,index){
return Padding(
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
child: Container(
decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(20.0)), color: Colors.white, boxShadow: [
BoxShadow(color: Colors.black.withAlpha(100), blurRadius: 5.0),
]),
child: ListTile(
isThreeLine: true,
title: Text('${experiences[index]['position']}'),
subtitle: Text('${experiences[index]['company']} - ${experiences[index]['duration']}'),
),
),
);
});
.....and goes on
这是 Firestore 中的数据树
【问题讨论】:
标签: arrays firebase flutter google-cloud-firestore