簡體   English   中英

從 Firestore 讀取 arrays 並插入 Flutter 中的列表

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

我在從 Firestore 讀取 arrays 時遇到問題,字符串可以在未來的構建器中讀取,如下所示,

但是,當讀取同一文檔中的數組字段並插入列表的時間不起作用時,實際上我無法使用 get 方法讀取數組。 嘗試了 [] 方法,但總是收到 null 錯誤,列出經驗 = snapshot.data.get('experiences') 不適用於 arrays,

從 firebase 讀取 arrays 是否有不同的方法? 我還需要讀取放入列表中的數組,並使用它將每個索引中的選定數據列出到列表視圖,就像我們通常在本地使用列表所做的那樣。

這是錯誤消息:

======== 小部件庫捕獲的異常====================================== ================== 在構建 FutureBuilder(dirty, state: _FutureBuilderState#395a0) 時拋出以下 NoSuchMethodError:在 null 上調用了方法“get”。 接收方:null 嘗試調用:get("experiences")

相關的導致錯誤的小部件是: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 StatefuldElement.gets/package:framework/rc .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 中的數據樹

Firestore 數據樹

您在 snapshot.connectionState 完成之前訪問了 snapshot.data。 此外,您忘記“返回”您的列表視圖。

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
} ....

這是您的問題中失敗代碼塊中的代碼,靠近未來構建器的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM