在 StreamBuilder 中渲染 ListView.ListTile
Render a ListView.ListTile in a StreamBuilder
我有一个 Flutter 应用程序,我试图在 StreamBuilder 中呈现一个 ListTile。
我在加载应用程序时遇到此错误:
type 'Future<List>' is not a subtype of type 'List'
neabyComp() 函数片段:
Stream nearbyComp() async* {
var pos = await location.getLocation();
GeoFirePoint point =
geo.point(latitude: pos.latitude, longitude: pos.longitude);
final CollectionReference users =
_firebaseFirestore.collection("Companies");
double radius = 10;
String field = 'location';
Stream<List<DocumentSnapshot>> stream = geo
.collection(collectionRef: users)
.within(center: point, radius: radius, field: field, strictMode: true);
yield stream;
}
这是错误来源的 StreamBuilder 代码片段:
Container(
child: StreamBuilder(
stream: nearbyComp(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Container(
child: new ListView(
children: snapshot.data.map((document) {
// Adding snapshot.data.map<Widget> also returns error "Future<List<Widget>> is not a subtype of type List<Widget>"
return new ListTile(
title: new Text(document['companyName']),
subtitle: new Image.network(document['url']),
);
}).toList(),
),
);
},
),
),
如果有人遇到这个问题,这里是@pskink 提供的解决方案(查看代码片段的注释):
Stream nearbyComp() async* {
var pos = await location.getLocation();
GeoFirePoint point =
geo.point(latitude: pos.latitude, longitude: pos.longitude);
final CollectionReference users =
_firebaseFirestore.collection("Companies");
double radius = 10;
String field = 'location';
Stream<List<DocumentSnapshot>> stream = geo
.collection(collectionRef: users)
.within(center: point, radius: radius, field: field, strictMode: true);
yield* stream; // In this line, add the * after yield
}
另一部分在这里:
Container(
child: StreamBuilder(
stream: nearbyComp(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Container(
child: new ListView(
children: snapshot.data.map((document) {
// Add snapshot.data.map<Widget>
return new ListTile(
title: new Text(document['companyName']),
subtitle: new Image.network(document['url']),
);
}).toList(),
),
);
},
),
),
我有一个 Flutter 应用程序,我试图在 StreamBuilder 中呈现一个 ListTile。
我在加载应用程序时遇到此错误:
type 'Future<List>' is not a subtype of type 'List'
neabyComp() 函数片段:
Stream nearbyComp() async* {
var pos = await location.getLocation();
GeoFirePoint point =
geo.point(latitude: pos.latitude, longitude: pos.longitude);
final CollectionReference users =
_firebaseFirestore.collection("Companies");
double radius = 10;
String field = 'location';
Stream<List<DocumentSnapshot>> stream = geo
.collection(collectionRef: users)
.within(center: point, radius: radius, field: field, strictMode: true);
yield stream;
}
这是错误来源的 StreamBuilder 代码片段:
Container(
child: StreamBuilder(
stream: nearbyComp(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Container(
child: new ListView(
children: snapshot.data.map((document) {
// Adding snapshot.data.map<Widget> also returns error "Future<List<Widget>> is not a subtype of type List<Widget>"
return new ListTile(
title: new Text(document['companyName']),
subtitle: new Image.network(document['url']),
);
}).toList(),
),
);
},
),
),
如果有人遇到这个问题,这里是@pskink 提供的解决方案(查看代码片段的注释):
Stream nearbyComp() async* {
var pos = await location.getLocation();
GeoFirePoint point =
geo.point(latitude: pos.latitude, longitude: pos.longitude);
final CollectionReference users =
_firebaseFirestore.collection("Companies");
double radius = 10;
String field = 'location';
Stream<List<DocumentSnapshot>> stream = geo
.collection(collectionRef: users)
.within(center: point, radius: radius, field: field, strictMode: true);
yield* stream; // In this line, add the * after yield
}
另一部分在这里:
Container(
child: StreamBuilder(
stream: nearbyComp(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Container(
child: new ListView(
children: snapshot.data.map((document) {
// Add snapshot.data.map<Widget>
return new ListTile(
title: new Text(document['companyName']),
subtitle: new Image.network(document['url']),
);
}).toList(),
),
);
},
),
),