FLUTTER:如何 return 只有我在 gridview 中想要的内容?
FLUTTER: How to return only what I want in gridview?
我正在使用 GridView
并且我尝试 return 仅当条件为真时才进行某些操作,但当条件为假时 return a CircularProgressIndicator
,如果我删除CircularProgressIndicator
我有一个白屏。你知道我怎样才能在 GridView
中只打印我想要的内容吗?
这是我的代码:
return new GridView.count(
crossAxisCount: 2,
children: snapshot.data.documents
.map((DocumentSnapshot document) {
if (document['id'] == nomines[0] ||
document['id'] == nomines[1])
return Container(
child: InkWell(
onTap: () {
vote(document['id']).then((a) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Waitresults(),
));
});
},
child: OvalPic(
document['photo'], document['couleur']),
),
); else
return CircularProgressIndicator();
}).toList());
你可以这样做:
snapshot.data.documents.map(
(d) => condition ? Container() : null
).toList()
..removeWhere((el) => el == null);
我假设你从 FutureBuilder
得到 snapshot
,试试这个,
FutureBuilder(
future: ...,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting)
return Center(child: CircularProgressIndicator());
else if (snapshot.hasError)
return Center(child: Text("ERROR: ${snapshot.error}"));
else {
final children = <Widget>[];
snapshot.data.documents.forEach((doc) {
if (document['id'] == nomines[0] ||
document['id'] == nomines[1])
children.add(
Container(
child: InkWell(
onTap: () {
vote(document['id']).then((a) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Waitresults(),
),
);
});
},
child: OvalPic(document['photo'], document['couleur']),
),
),
);
});
return new GridView.count(
crossAxisCount: 2,
children: children,
);
}
},
);
我正在使用 GridView
并且我尝试 return 仅当条件为真时才进行某些操作,但当条件为假时 return a CircularProgressIndicator
,如果我删除CircularProgressIndicator
我有一个白屏。你知道我怎样才能在 GridView
中只打印我想要的内容吗?
这是我的代码:
return new GridView.count(
crossAxisCount: 2,
children: snapshot.data.documents
.map((DocumentSnapshot document) {
if (document['id'] == nomines[0] ||
document['id'] == nomines[1])
return Container(
child: InkWell(
onTap: () {
vote(document['id']).then((a) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Waitresults(),
));
});
},
child: OvalPic(
document['photo'], document['couleur']),
),
); else
return CircularProgressIndicator();
}).toList());
你可以这样做:
snapshot.data.documents.map(
(d) => condition ? Container() : null
).toList()
..removeWhere((el) => el == null);
我假设你从 FutureBuilder
得到 snapshot
,试试这个,
FutureBuilder(
future: ...,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting)
return Center(child: CircularProgressIndicator());
else if (snapshot.hasError)
return Center(child: Text("ERROR: ${snapshot.error}"));
else {
final children = <Widget>[];
snapshot.data.documents.forEach((doc) {
if (document['id'] == nomines[0] ||
document['id'] == nomines[1])
children.add(
Container(
child: InkWell(
onTap: () {
vote(document['id']).then((a) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Waitresults(),
),
);
});
},
child: OvalPic(document['photo'], document['couleur']),
),
),
);
});
return new GridView.count(
crossAxisCount: 2,
children: children,
);
}
},
);