【问题标题】:Retrieve Data from Firestore and Pass it to a variable. Flutter从 Firestore 中检索数据并将其传递给变量。扑
【发布时间】:2020-12-23 17:16:39
【问题描述】:

您好,我想问一下如何从 Firestore 中检索数据并将其发送给替身。

这是我从 Firestore 中检索数据的代码。

Firestore.instance
            .collection('locations')
            .snapshots()
            .listen((driverLocation){
          driverLocation.documents.forEach((dLocation){
            dLocation.data['Latitude'] = latitude;
            dLocation.data['Longitude'] = longitude;
            print(latitude);
          });
        });

我将它存储在dLocation 中,当我使用print(dLocation.data) 时,它将在 Firestore 中显示纬度和经度。但是当我将它传递给double latitudedouble longitude 时,它返回null。

busStop.add(
          Marker(
            markerId: MarkerId('driverLocation')
            ,
            draggable: false,
            icon: BitmapDescriptor.defaultMarker,
            onTap: () {
            },
            position: LatLng(latitude, longitude),
          ));

然后我想将double latitudedouble longitude 中的数据传递到标记中,以便标记将相应地移动到 Firestore 中的纬度和经度。

这里发生的一切都在initState() 中。

**如果您有什么想问的,请随时这样做,因为我不知道如何表达我的问题。非常感谢您。

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    你做错了。现在,您将latitude 的值(为空)分配给dLocation.data['latitude'] 的值。你想做的是这样的:

    latitude = dLocation.data['latitude'];
    longitude = dLocation.data['longitude'];
    

    通过此更改,dLocation.data['latitude'] 的值将分配给 latitudedLocation.data['longitude'] 的值将分配给 longitude 变量

    更新:

    要获取新标记并在屏幕上显示具有latitudelongitude 值的标记,您可以执行以下操作:

    @override
    void initState(){
    Firestore.instance
                .collection('locations')
                .snapshots()
                .listen((driverLocation){
              //busStop = []; removes all the old markers and you don't get duplicate markers with different coordinates
              driverLocation.documents.forEach((dLocation){
                dLocation.data['Latitude'] = latitude;
                dLocation.data['Longitude'] = longitude;
                busStop.add(
                   Marker(
                      markerId: MarkerId('driverLocation')
                      ,
                      draggable: false,
                      icon: BitmapDescriptor.defaultMarker,
                      onTap: () {
                      },
                      position: LatLng(latitude, longitude),
              )); //you need to check for duplicate markers here. you can do it by giving each marker an unique id and check for the same marker in the list so you don't get duplicate markers.
              });
             setState((){}); //rebuilds the widget tree after adding the markers to the busStop list
            });
    
    }
    

    这里发生的事情是您将标记添加到 busStop 列表,在添加所有标记后,您调用 setState 并且小部件树使用最新数据重建屏幕。您可能需要检查重复标记,因为它们可能会重新添加到 busStop 列表中。或者您可以简单地删除所有旧标记并使用busStop = []; 添加新标记,然后再添加到busStop

    【讨论】:

    • 非常感谢,这帮助我解决了将dLocation 数据发送到longitudelatitude 的问题,但是当我在Marker 函数中使用该变量时,它再次返回null。我不知道怎么做。给您带来不便深表歉意。
    • 没问题。您可以在将值分配给纬度和经度后调用 setState(),以便使用新值再次构建小部件树
    • 所以你的意思是我需要在 initState() 中调用 setState()?
    • 有什么例子吗?我在互联网上搜索并没有找到太多,因为我不太了解它的含义。
    • 为了对 UI 进行任何更改,您需要调用 setState。如果您正在使用 busStop 列表构建任何小部件,为了获得包含最新项目的最新列表,您需要以某种方式对其进行更新,以便调用 setState。因此,您的小部件树将使用 busStop 列表的最新项目再次创建。
    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 2021-08-01
    • 1970-01-01
    • 2018-11-24
    • 2023-03-31
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多