You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Flutter将LatLng对象保存到Firestore的参数无效错误

解决Firestore保存LatLng对象的错误

错误Invalid argument: Instance of 'LatLng'的原因是Firestore无法直接序列化latlong2库的LatLng自定义对象,必须将其转换为Firestore支持的数据类型(比如Map或GeoPoint)才能存储。

方案一:将LatLng转为Map存储

这种方式会在Firestore中以普通键值对形式保存经纬度,适合简单存储场景:

修改你的ActivitiesModel类的toJson和fromJson方法:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:latlong2/latlong.dart';

class ActivitiesModel {
  String id;
  String organisateur;
  final DateTime? createDate;
  final DateTime? modifyDate;
  LatLng location;

  ActivitiesModel({
    this.id = '',
    required this.organisateur,
    this.createDate,
    this.modifyDate,
    required this.location,
  });

  Map<String, dynamic> toJson() => {
        'id': id,
        'organisateur': organisateur,
        'createDate': createDate,
        'modifyDate': modifyDate,
        // 将LatLng转为包含lat和lng的Map
        'location': {
          'lat': location.latitude,
          'lng': location.longitude,
        },
      };

  static ActivitiesModel fromJson(Map<String, dynamic> json) => ActivitiesModel(
        id: json['id'],
        organisateur: json['organisateur'],
        createDate: (json['createDate'] as Timestamp).toDate(),
        modifyDate: (json['modifyDate'] as Timestamp).toDate(),
        // 从Map还原LatLng
        location: LatLng(
          json['location']['lat'] as double,
          json['location']['lng'] as double,
        ),
      );
}

方案二:使用Firestore的GeoPoint类型存储

这种方式会在Firestore中存储为地理点类型,支持后续的地理查询(比如查找附近地点),推荐需要地理功能的场景使用:

修改模型类如下:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:latlong2/latlong.dart';

class ActivitiesModel {
  String id;
  String organisateur;
  final DateTime? createDate;
  final DateTime? modifyDate;
  LatLng location;

  ActivitiesModel({
    this.id = '',
    required this.organisateur,
    this.createDate,
    this.modifyDate,
    required this.location,
  });

  Map<String, dynamic> toJson() => {
        'id': id,
        'organisateur': organisateur,
        'createDate': createDate,
        'modifyDate': modifyDate,
        // 将LatLng转为Firestore的GeoPoint
        'location': GeoPoint(location.latitude, location.longitude),
      };

  static ActivitiesModel fromJson(Map<String, dynamic> json) => ActivitiesModel(
        id: json['id'],
        organisateur: json['organisateur'],
        createDate: (json['createDate'] as Timestamp).toDate(),
        modifyDate: (json['modifyDate'] as Timestamp).toDate(),
        // 从GeoPoint还原LatLng
        location: LatLng(
          (json['location'] as GeoPoint).latitude,
          (json['location'] as GeoPoint).longitude,
        ),
      );
}

保存代码无需修改

你现有的保存代码可以直接使用,模型类会自动完成LatLng与Firestore支持类型的转换:

final activitiesModel = ActivitiesModel(
  id: widget.activitiesModel.id,
  organisateur: 'Mickael R.',
  createDate: widget.activitiesModel.createDate,
  modifyDate: DateTime.now(),
  location: LatLng(-12.0080041, -77.0778237),
);
// 直接将activitiesModel.toJson()传入Firestore的set或add方法即可

内容的提问来源于stack exchange,提问作者Mickael Roignant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 15:17:22