无法从GeoFlutterFire获取用户数据

时间:2020-08-13 12:42:42

标签: flutter

我想显示从geoflutterfire的地理查询中获得的用户信息,就像我根据用户名查询数据库时一样。我尝试对geoquery的数据执行相同的操作,但未显示任何内容。我是新手,但我听不到监听功能。

    Future<QuerySnapshot> allUsers = usersReference
        .where("username",
            isGreaterThanOrEqualTo: str,
            isLessThan: str.substring(0, str.length - 1) +
                String.fromCharCode(str.codeUnitAt(str.length - 1) + 1))
        .getDocuments();
    setState(() {
      futureSearchResults = allUsers;
    });
  }
displayUsersFoundScreen() {
    return FutureBuilder(
        future: futureSearchResults,
        builder: (context, dataSnapshot) {
          if (!dataSnapshot.hasData) {
            return circularProgress();
          }

          List<UserResult> searchUsersResult = [];
          dataSnapshot.data.documents.forEach((document) {
            User eachUser = User.fromDocument(document);
            UserResult userResult = UserResult(eachUser);
            searchUsersResult.add(userResult);
          });
          return ListView(
            children: searchUsersResult,
          );
        });
  }
class UserResult extends StatelessWidget {
  final User eachUser;
  UserResult(this.eachUser);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(3.0),
      child: Container(
        color: Colors.grey,
        child: Column(
          children: <Widget>[
            GestureDetector(
              onTap: () => displayUserProfile(
                context,
                userProfileId: eachUser.id,
              ),
              child: ListTile(
                leading: CircleAvatar(
                  backgroundColor: Colors.black,
                  backgroundImage: CachedNetworkImageProvider(eachUser.url),
                ),
                title: Text(
                  eachUser.profileName,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16.0,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                subtitle: Text(
                  eachUser.username,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 13.0,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }} 

searchNearbyUsers() async{
    Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.best);
    double lat = position.latitude;
    double lng = position.longitude;
    GeoFirePoint  center = Geoflutterfire().point(latitude: lat, longitude:lng);

    var ref = _firestore.collection('users');

    double rad = 50;
    String field = 'userLocation';

    Stream<List<DocumentSnapshot>> stream = Geoflutterfire().collection(collectionRef: ref)
                                        .within(center: center, radius: rad, field: field);
stream.listen((List<DocumentSnapshot> documentList) {
       print(documentList);
      });
  }
  displayNoSearchResultScreen() {
    return StreamBuilder(stream: stream,
        builder: (context, dataSnapshot) {
          if (!dataSnapshot.hasData) {
            return circularProgress();
          }
          List<UserResult> nearbyUsersResult = [];
          dataSnapshot.data.documents.forEach((document) {
            User eachUser = User.fromDocument(document);
            UserResult userResult = UserResult(eachUser);
            nearbyUsersResult.add(userResult);
          });
         return ListView(
            children: nearbyUsersResult,
          );
        });
  }

0 个答案:

没有答案