简体   繁体   中英

How to use StreamBuilder?

I tried implementing this code in my project, here is the link to that https://gitmemory.com/issue/DarshanGowda0/GeoFlutterFire/16/472098428

This is my Scaffold.

Scaffold(
          body: Center(
        child: Column(
          children: <Widget>[
            FloatingActionButton(
              onPressed: () async  {

             await GetusersLocation();

              },
              child: Center(
                child: Icon(
                  Icons.add,
                ),
              ),
            ),

            StreamBuilder(
              stream: stream,
              builder: (BuildContext context,
                  AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
                if (snapshots.connectionState == ConnectionState.active &&
                    snapshots.hasData) {
                  print(snapshots.data);
                  return Container();
                } else {
                  return Center(child: CircularProgressIndicator());
                }
              },
            ),

          ],
        ),
      )),

Here is my GetusersLocation()

Future<void> GetusersLocation() async {
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    lat = position.latitude;
    long = position.longitude;
  }

Here is my initState()


Stream<List<DocumentSnapshot>> stream;



  @override
  void initState() {
    super.initState();
    geo = Geoflutterfire();
    var radius = BehaviorSubject.seeded(1.0);
    GeoFirePoint center = geo.point(latitude: 26.8462924, longitude: 81.0042322);
    stream = radius.switchMap((rad) {
      var collectionReference = _firestore.collection('locations');
      return geo.collection(collectionRef: collectionReference).within(
          center: center, radius: rad, field: 'position', strictMode: true);
    });
  }

I'm getting this printed in my console.

[Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot'].

Please help me out.

You got a List of DocumentSnapshot which is normal, your data is in the DocumentSnapshot. You can use the List of DocumentSnapshot in a ListView like so.

 StreamBuilder(
    stream: stream,
    builder: (BuildContext context,
        AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
      if (snapshots.connectionState == ConnectionState.active &&
          snapshots.hasData) {
        print(snapshots.data);
        return ListView.builder(
          itemCount: snapshots.data.length,
          itemBuilder: (BuildContext context, int index) {
            DocumentSnapshot doc = snapshots.data[index];
            Map location = doc.data; // this is your data which is probably a map
            return Text(
              location.toString(),
            );
          },
        );
      } else {
        return Center(child: CircularProgressIndicator());
      }
    },
  ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM