flutter - 我无法显示我的 Firebase 数据库的标记

我真的迷路了,如果您发现另一个错误或不正确的做事方式,我不知道如何解决问题我对建议感兴趣

这是我制作的 firebase 阅读代码

//############################
//# Markers Builder FireBase #
//############################

  Map<MarkerId, Marker> markers = <MarkerId, Marker>{};

  getMarkerData() async {
    FirebaseFirestore.instance.collection('Markers').get().then((myMarkers) {
      if (myMarkers.docs.isNotEmpty) {
        for (int i = 0; i < myMarkers.docs.length; i++) {
          initMarker(myMarkers.docs[i].data, myMarkers.docs[i].id);
        }
      }
    });
  }

  void initMarker(specify, specifyId) {
    var markerIdVal = specifyId;
    final MarkerId markerId = MarkerId(markerIdVal);
    final Marker marker = Marker(
      markerId: markerId,
      position:
          LatLng(specify['Location'].latitude, specify['Location'].longitude),
      infoWindow: InfoWindow(snippet: specify['Test data Firebase Marker']),
    );
    setState(() {
      markers[markerId] = marker;
    });
  }

这是错误消息

Restarted application in 654ms.
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: NoSuchMethodError: Closure call with mismatched arguments: function '[]'
Receiver: Closure: () => Map<String, dynamic> from Function 'data':.
Tried calling: []("Location")
Found: []() => Map<String, dynamic>
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1      PageMapsState.initMarker
#2      PageMapsState.getMarkerData.<anonymous closure>
package:noname/views/mappage.dart:144
#3      _rootRunUnary (dart:async/zone.dart:1434:47)
#4      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
<asynchronous suspension>
Application finished.

这是完整的代码

import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:noname/blocs/geolocation/geolocation_bloc.dart';
import 'package:noname/onboarding/onboarding_font.dart';
import 'package:noname/widget/others/utils.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';

class PageMaps extends StatefulWidget {
  @override
  PageMapsState createState() => PageMapsState();
}
//###########################
//# Initialisation générale #
//###########################

class UserLocation {
  final double latitude;
  final double longitude;
  UserLocation(this.latitude, this.longitude);
}

class PageMapsState extends State<PageMaps> {
  Completer<GoogleMapController> _controller = Completer();

  @override
  void initState() {
    getMarkerData();
    super.initState();
  }

//##########################
//# Launch Widget générale #
//##########################

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          _BottomsSliderInfo(),
          _Searchbar(),
        ],
      ),
    );
  }

//#####################
//# Widget Search bar #
//#####################

  Widget _Searchbar() {
    return SafeArea(
        child: SizedBox(
      width: 900.0,
      height: 60.0,
      child: Card(
        child: Center(
          child: Text(
            'Search bar a DEV!! + Bouton filtre',
            style: temp,
          ),
        ),
        color: Colors.blue,
      ),
    ));
  }

//###########################
//# Widget SliderInfoBottom #
//###########################

  Widget _BottomsSliderInfo() {
    return Scaffold(
      body: SlidingUpPanel(
          panel: Center(
            child: Text(
              "Information a développer",
              style: temp,
            ),
          ),
          body: Stack(
            children: <Widget>[
              _buildGoogleMap(context),
            ],
          )),
    );
  }

//###################################
//# Widget Constructeur Google Maps #
//###################################

  Widget _buildGoogleMap(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height,
      width: MediaQuery.of(context).size.width,
      child: BlocBuilder<GeolocationBloc, GeolocationState>(
        builder: (context, state) {
          if (state is GeolocationLoading) {
            return Center(
              child: CircularProgressIndicator(),
            );
          } else if (state is GeolocationLoaded) {
            return GoogleMap(
              initialCameraPosition: CameraPosition(
                  target:
                      LatLng(state.position.latitude, state.position.longitude),
                  zoom: 14.5),
              onMapCreated: (GoogleMapController controller) {
                _controller.complete(controller);
                controller.setMapStyle(Utils.MapStyle);
              },
              myLocationButtonEnabled: false,
              minMaxZoomPreference: MinMaxZoomPreference(8, 25),
              myLocationEnabled: true,
              markers: Set<Marker>.of(markers.values),
              // markers: {
              //   Bruxelles_central,
              //   paparoti,
              //   test,
              // },
            );
          } else {
            return Text('Erreur de localisation');
          }
        },
      ),
    );
  }
//############################
//# Markers Builder FireBase #
//############################

  Map<MarkerId, Marker> markers = <MarkerId, Marker>{};

  getMarkerData() async {
    FirebaseFirestore.instance.collection('Markers').get().then((myMarkers) {
      if (myMarkers.docs.isNotEmpty) {
        for (int i = 0; i < myMarkers.docs.length; i++) {
          initMarker(myMarkers.docs[i].data, myMarkers.docs[i].id);
        }
      }
    });
  }

  void initMarker(specify, specifyId) {
    var markerIdVal = specifyId;
    final MarkerId markerId = MarkerId(markerIdVal);
    final Marker marker = Marker(
      markerId: markerId,
      position:
          LatLng(specify['Location'].latitude, specify['Location'].longitude),
      infoWindow: InfoWindow(snippet: specify['Test data Firebase Marker']),
    );
    setState(() {
      markers[markerId] = marker;
    });
  }

  // Set<Marker> getMarker() {
  //   return <Marker>[
  //     Marker(
  //       markerId: MarkerId('Test'),
  //       position: LatLng(37.783739514044825, -122.4084728076636),
  //       icon: BitmapDescriptor.defaultMarker,
  //     )
  //   ].toSet();
  // }
}

我发现了很多关于这个主题的帖子,但没有任何效果 感谢您阅读帖子

回答1

尝试这个:

getMarkerData() async {
    FirebaseFirestore.instance.collection('Markers').get().then((DocumentSnapshot myMarkers) {
      if (myMarkers.exists) {
        var data = myMarkers.data();
        var res = data as Map<String, dynamic>;
        res.forEach((e){
          //initMarker();
        });
      }
    });
  }

相似文章

最新文章