【问题标题】:Not receiving the updated data from firebase in the flutter app在颤振应用程序中未收到来自 Firebase 的更新数据
【发布时间】:2020-11-14 17:02:16
【问题描述】:

我正在尝试在使用颤振实时数据库的颤振应用程序中进行实时跟踪。为了测试代码,我直接在 firebase 中更改数据并检查它是否已在控制台中打印。我需要根据来自 Firebase 的位置更新来更新标记。但是,我无法弄清楚为什么我的应用没有收听数据。

这是我的代码。

class _TrackMapState extends State<TrackMap> {
  GlobalKey<ScaffoldState> _key = new GlobalKey<ScaffoldState>();
  List<Marker> _allMarkers = [];

  final firebaseDB = FirebaseDatabase.instance.reference();

  @override
  void initState() {
    super.initState();
    _allMarkers.add(Marker(
        markerId: MarkerId("Destination"),
        draggable: false,
        position: LatLng(widget.model.requests[widget.index]["latitude"],
            widget.model.requests[widget.index]["longitude"])));

    firebaseDB
        .child(widget.model.requests[widget.index]["assignedAgent"])
        .once()
        .then((DataSnapshot data) {
      setState(() {
        _allMarkers.add(Marker(
            markerId: MarkerId("agent"),
            draggable: false,
            position: LatLng(data.value["latitude"], data.value["longitude"])));
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _key,
      appBar: CustomAppBar("Requests"),
      body: Container(
        decoration: BoxDecoration(
            border: Border.all(width: 2, color: Colors.grey[300])),
        child: GoogleMap(
          initialCameraPosition: CameraPosition(
            target: LatLng(widget.model.requests[widget.index]["latitude"],
                widget.model.requests[widget.index]["longitude"]),
            zoom: 9,
          ),
          markers: Set.from(_allMarkers),
        ),
      ),
      drawer: SideDrawer(),
      floatingActionButton: FloatingActionButton(
        onPressed: _trackCollectionAgent,
        child: Icon(Icons.location_searching),
      ),
    );
  }

  Future<void> _trackCollectionAgent() async {
    firebaseDB
        .child(widget.model.requests[widget.index]["assignedAgent"])
        .onChildChanged
        .listen((event) {
      var key = event.snapshot.key;
      var value = event.snapshot.value;

      // not sure what data I'm going to get here and 
      //so trying to print it here
      print(key);
      print(value);

      Marker _marker = Marker(
          markerId: MarkerId("agent"),
          draggable: false,
          position: (key == "latitude")
              ? LatLng(value, _allMarkers[1].position.longitude)
              : LatLng(_allMarkers[1].position.latitude, value));
      setState(() {
        _allMarkers[1] = _marker;
        print(_allMarkers);
      });
    });
  }
}

我在这里做错了什么吗?

提前致谢。

【问题讨论】:

    标签: flutter dart firebase-realtime-database


    【解决方案1】:

    这是我使用的解决方法。

    void initState() {
        ....
    
        setUpTimedFetch();
      }
    
      setUpTimedFetch() {
        Timer.periodic(Duration(milliseconds: 5000), (timer) {
            _future = Future.value(timer.tick.toString());
            firebaseDB
                .child(widget.data["assignedAgent"])
                .once()
                .then((DataSnapshot data) {
              print(data.value);
              Marker _marker = Marker(
                markerId: MarkerId("agent"),
                draggable: false,
                icon:
                    BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
                infoWindow: InfoWindow(
                    title: "Agent",
                    snippet:
                        "(${data.value['latitude']}, ${data.value['longitude']})"),
                position: LatLng(data.value["latitude"], data.value["longitude"]),
              );
              setState(() {
                _allMarkers[1] = _marker;
                print(_allMarkers);
              });
            });
        });
      }
    

    【讨论】:

      猜你喜欢
      • 2022-10-09
      • 2021-04-07
      • 1970-01-01
      • 2022-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      相关资源
      最近更新 更多