[英]Flutter - How to map List of array to Widget
我想將數組數據從 MongoDB 傳遞到小部件列表。 但是,我收到“類型 'MappedListIterable<ApplicantsList, Widget>' 不是類型 'List' 的子類型”的錯誤。 有人能幫我嗎?
這是我的變量:
List<ApplicantsList> applicants;
var store, store1;
申請人 class / 構造函數:
class ApplicantsList {
String name;
String contact;
ApplicantsList({this.name, this.contact});
}
從 mongodb 獲取數據。 (工作正常。我可以得到數據。)
Future <void> getApplicantInfo() async {
AuthService().getRequestorApplicants().then((val) async {
store = val.data[0];
store1 = val.data[1];
for (var i = 0; i < 2; i++) {
applicants =[ ApplicantsList(name: store['parentname'], contact:
store['location']) ];
}
});
}
這是我認為我得到錯誤的地方。 將數據傳遞給 _PendingCardDonor 小部件時。
Container(
child: StreamBuilder<Object>(
stream: null,
builder: (context, snapshot) {
return Container(
height: MediaQuery.of(context).size.height,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: <Widget>[
Column(
children: applicants.map((e) => _PendingCardDonor(e.name, e.contact,"Balubad, Mindoro"),),,
],
));
}
),
),
_PendingCardDonor 小部件
Widget _PendingCardDonor (String name, String age, String location) {
return InkWell(
onTap: () {
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
boxShadow: [
BoxShadow(
color: getColorFromHex('#a19c9c'),
offset: Offset(-1, 2),
blurRadius: 4,
),
],
),
margin: EdgeInsets.only(left: 10.0, right: 10.0, top: 10.0),
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(bottom: 5.0),
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: getColorFromHex('#faf7f7'),
borderRadius: BorderRadius.all(Radius.circular(5)),
),
child: Row(
children: [
Icon(Icons.person, color: Colors.black,),
SizedBox(
width: 5,
),
Flexible(
flex: 1,
child: Column(
children: [
Container(
child: AutoSizeText(name,
style: TextStyle(color: Colors.black, fontSize: 16,),
minFontSize: 13,
stepGranularity: 1,
maxLines:2),
),
],
),
),
],
),
),
Container(
margin: EdgeInsets.only(bottom: 5.0),
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: getColorFromHex('#faf7f7'),
borderRadius: BorderRadius.all(Radius.circular(5)),
),
child: Row(
children: [
Icon(Icons.calendar_today_rounded, color: Colors.black, ),
SizedBox(
width: 5,
),
Text(age + " yrs old",
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 16.0,
color: Colors.black,
)),
],
),
),
Container(
margin: EdgeInsets.only(bottom: 5.0),
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: getColorFromHex('#faf7f7'),
borderRadius: BorderRadius.all(Radius.circular(5)),
),
child: Row(
children: [
Icon(Icons.location_pin, color: Colors.black,),
SizedBox(
width: 5,
),
Flexible(
child: Column(
children: [
Container(
child: AutoSizeText(location,
style: TextStyle(color: Colors.black, fontSize: 16, ),
minFontSize: 13,
stepGranularity: 1,
maxLines:3),
),
],
),
),
],
),
),
Container(
width: MediaQuery.of(context).size.width,
child: RaisedButton(
elevation: 0.0,
color: getColorFromHex('#FE9C8F'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
onPressed: () {
Navigator.push(context, PageTransition(type: PageTransitionType.leftToRight, child: ViewPendingApplicant(
upass: widget.upass,
email: widget.email,
name: name,
age: age,
location: location,
)));
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
Icons.folder_open,
color: Colors.white,
),
Padding(
padding: EdgeInsets.only(left: 10.0),
child: Text(
"OPEN",
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.white),
)),
],
),
),
),
],
),
),
);
}
看起來您可以在applicants.map
者之后添加.toList()
如下所示:
Container(
child: StreamBuilder<Object>(
stream: null,
builder: (context, snapshot) {
return Container(
height: MediaQuery.of(context).size.height,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: <Widget>[
Column(
children: applicants
.map((e) => _PendingCardDonor(e.name, e.contact,"Balubad, Mindoro")),
.toList() // <-- HERE
),
]
)
);
}
),
),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.