Здравствуйте, я пытаюсь добавить в свою коллекцию документ с именем 'fitbit_activity', который имеет массив расстояний. Это модель, которую я сделал самостоятельно на основе JSON Fitbit. Вот как выглядит модель:
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
Должен ли я написать цикл внутри карты, чтобы я мог добавить его в свою базу данных? Любая помощь будет принята с благодарностью! Заранее спасибо!