gpt4 book ai didi

firebase - 向Cloud Firestore添加阵列(Flutter/Dart)

转载 作者:行者123 更新时间:2023-12-03 03:38:52 25 4
gpt4 key购买 nike

您好,我正在尝试将名为“fitbit_activity”的文档添加到具有距离数组的“fitbit_activity”集合中,这是我根据Fitbit的JSON自行创建的模型。因此,该模型如下所示:

class Distances {
String activity;
var distance;

Distances({this.activity, this.distance});

factory Distances.fromJson(Map<String, dynamic> parsedJson) {
return Distances(
activity: parsedJson['activity'],
distance: parsedJson['distance'].toDouble());
}
}

Here is what the API Response for 'Distances' Look like :
    "distances": [
{
"activity": "total",
"distance": 5.94
},
{
"activity": "tracker",
"distance": 5.94
},
{
"activity": "loggedActivities",
"distance": 0
},
{
"activity": "veryActive",
"distance": 1.92
},
{
"activity": "moderatelyActive",
"distance": 0.46
},
{
"activity": "lightlyActive",
"distance": 3.26
},
{
"activity": "sedentaryActive",
"distance": 0.23
}
],

最后,这是我的代码,将数据添加到我的firestore数据库中:
db = Firestore.instance();
await db
.collection('users')
.document(userData[index].uID)
.collection('fitbit_activity')
.document(DateFormat.yMMMd().format(today))
.setData({
'caloriesBMR': _fitbitActivityApiVar.summary.caloriesBMR,
'caloriesBurned': _fitbitActivityApiVar.summary.caloriesBurned,
'fairlyActiveMinutes': _fitbitActivityApiVar.summary.lightlyActiveMinutes,
'lightlyActiveMinutes': _fitbitActivityApiVar.summary.fairlyActiveMinutes,
'veryActiveMinutes': _fitbitActivityApiVar.summary.veryActiveMinutes,
'sedentaryMinutes': _fitbitActivityApiVar.summary.sedentaryMinutes,
'steps': _fitbitActivityApiVar.summary.steps,
'restingHeartRate': _fitbitActivityApiVar.summary.restingHeartRate,
'distances': _fitbitActivityApiVar.summary.distances //I am getting an error here
});

这是错误:
E/flutter ( 4149): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Invalid argument: Instance of 'Distances'
E/flutter ( 4149): #0 StandardMessageCodec.writeValue
package:flutter/…/services/message_codecs.dart:392
E/flutter ( 4149): #1 FirestoreMessageCodec.writeValue
package:cloud_firestore/src/firestore_message_codec.dart:64
E/flutter ( 4149): #2 StandardMessageCodec.writeValue
package:flutter/…/services/message_codecs.dart:382
E/flutter ( 4149): #3 FirestoreMessageCodec.writeValue
package:cloud_firestore/src/firestore_message_codec.dart:64
E/flutter ( 4149): #4 StandardMessageCodec.writeValue.<anonymous closure>
package:flutter/…/services/message_codecs.dart:389
E/flutter ( 4149): #5 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter ( 4149): #6 StandardMessageCodec.writeValue
package:flutter/…/services/message_codecs.dart:387
E/flutter ( 4149): #7 FirestoreMessageCodec.writeValue
package:cloud_firestore/src/firestore_message_codec.dart:64
E/flutter ( 4149): #8 StandardMessageCodec.writeValue.<anonymous closure>
package:flutter/…/services/message_codecs.dart:389
E/flutter ( 4149): #9 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter ( 4149): #10 StandardMessageCodec.writeValue
package:flutter/…/services/message_codecs.dart:387
E/flutter ( 4149): #11 FirestoreMessageCodec.writeValue
package:cloud_firestore/src/firestore_message_codec.dart:64
E/flutter ( 4149): #12 StandardMethodCodec.encodeMethodCall
package:flutter/…/services/message_codecs.dart:524
E/flutter ( 4149): #13 MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:311
E/flutter ( 4149): <asynchronous suspension>
E/flutter ( 4149): #14 DocumentReference.setData
package:cloud_firestore/src/document_reference.dart:51

我应该在 map 内编写一个循环,以便将其添加到数据库中吗?任何帮助将不胜感激!先感谢您!

最佳答案

好像您缺少Distances类的序列化方法。

添加执行此操作的toJson方法:

class Distances {
...
Map<String, dynamic> toJson() {
return {
"activity": activity,
"distance": distance,
};
}
}

然后,在将数据存储到Firestore之前,必须为数组中的每个项目添加对该方法的调用

 ...
'distances': _fitbitActivityApiVar
.summary
.distances
.map((distance) => distance.toJson())
.toList(),
});

希望对你有用

关于firebase - 向Cloud Firestore添加阵列(Flutter/Dart),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58731742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com