在 StreamBuilder 中渲染 ListView.ListTile

Posted

技术标签:

【中文标题】在 StreamBuilder 中渲染 ListView.ListTile【英文标题】:Render a ListView.ListTile in a StreamBuilder 【发布时间】:2021-08-25 00:25:12 【问题描述】:

我有一个 Flutter 应用程序,我试图在 StreamBuilder 中呈现 ListTile。

加载应用时出现此错误:

type 'Future' 不是 type 'List' 的子类型

neabyComp() 函数 sn-p:

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 代码的 sn-p:

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

【问题讨论】:

你应该试试 listView.builder,它会接受未来列表 @pskink 写在评论的那一行,children:snapshot.data.map((document)... builder: 的第一行添加print(snapshot);,您在日志中看到了什么? snapshot.data.map -> snapshot.data.map&lt;Widget&gt; 2 个月???你是说2天?无论如何,如果您愿意,请随时发布自己的答案 【参考方案1】:

如果有人遇到这个问题,这里是@pskink提供的解决方案(寻找代码sn-ps上的cmets):

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

【讨论】:

以上是关于在 StreamBuilder 中渲染 ListView.ListTile的主要内容,如果未能解决你的问题,请参考以下文章

Streambuilder 渲染两次,第一次使用初始数据,第二次使用正确数据,但小部件不更新

Flutter 中的 StreamBuilder 卡在 ConnectionState.waiting 中并且只显示加载标记

来自一个 StreamBuilder 的 Flutter 快照显示在另一个 StreamBuilder 中

在 StreamBuilder 中使用 AnimatedList

如何在streambuilder中查询firestore文档并更新listview

在 Flutter 中探索 StreamBuilder