简体   繁体   English

从 Firestore 读取 arrays 并插入 Flutter 中的列表

[英]Reading arrays from Firestore and inserting to a list in Flutter

Im having problem reading arrays from Firestore, strings can be readable within future builder as below,我在从 Firestore 读取 arrays 时遇到问题,字符串可以在未来的构建器中读取,如下所示,

How ever when its time to read array field in very same document and insert in a list is just not working, actually Im not able to read the array with get method.但是,当读取同一文档中的数组字段并插入列表的时间不起作用时,实际上我无法使用 get 方法读取数组。 tried [] method but always receving getting null error, List experiences = snapshot.data.get('experiences') is not working for arrays,尝试了 [] 方法,但总是收到 null 错误,列出经验 = snapshot.data.get('experiences') 不适用于 arrays,

Is there a different method for reading arrays from firebase?从 firebase 读取 arrays 是否有不同的方法? I also need to read that array put in a list and use it to list the selected data from each index to a listview as usually we do locally with lists.我还需要读取放入列表中的数组,并使用它将每个索引中的选定数据列出到列表视图,就像我们通常在本地使用列表所做的那样。

Here is the error message:这是错误消息:

======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#395a0): The method 'get' was called on null. ======== 小部件库捕获的异常====================================== ================== 在构建 FutureBuilder(dirty, state: _FutureBuilderState#395a0) 时抛出以下 NoSuchMethodError:在 null 上调用了方法“get”。 Receiver: null Tried calling: get("experiences")接收方:null 尝试调用:get("experiences")

The relevant error-causing widget was: FutureBuilder file:///xxxxxxxx/lib/regformSummary.dart:185:45 When the exception was thrown, this was the stack: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 _RegFormSummaryState.build.相关的导致错误的小部件是:FutureBuilder file:///xxxxxxxx/lib/regformSummary.dart:185:45 当抛出异常时,这是堆栈:#0 Object.noSuchMethod (dart:core-patch/object_patch. dart:54:5) #1 _RegFormSummaryState.build。 (package:xxxxxx/regformSummary.dart:186:94) #2 _FutureBuilderState.build (package: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)... (package:xxxxxx/regformSummary.dart:186:94) #2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:773:55) #3 StatefuldElement.gets/package:framework/rc .dart:4612:27) #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)...

here is working code for reading strings这是用于读取字符串的工作代码

 //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(); }),

Here is the failing code这是失败的代码

 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

and here is the data tree in Firestore这是 Firestore 中的数据树

Firestore data tree Firestore 数据树

You accessed snapshot.data before snapshot.connectionState was done.您在 snapshot.connectionState 完成之前访问了 snapshot.data。 Moreover, you forgot to 'return' your list view.此外,您忘记“返回”您的列表视图。

if (snapshot.connectionState == ConnectionState.done) {
    List experiences = List.from(snapshot.data.get('experiences')); // This line should be inside if block
    return ListView.builder(...); // You forgot to add 'return' here
} ....

This is the code inside your failing code block in your question near the end inside the future builder.这是您的问题中失败代码块中的代码,靠近未来构建器的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM