1

Hello I'm trying to add a document to my collection named 'fitbit_activity' that has an array of Distances, this is a model I made on my own based on the JSON of Fitbit. So here is how the model looks like:

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
        }
    ],

And Finally here is my code to add the data to my firestore db:

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
    });

Here is the error:

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

Should I write a loop inside the map, so I can add it to my database? Any help would be greatly appreciated! Thank you in Advance!

1 Answer 1

1

Looks like you're missing a serialization method for your Distances class.

Add a toJson method that does this:

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

Then you have to add a call to that method for each item in your array before you store the data to Firestore

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

Hope that works for you

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! this works! Should I add a serialization method for all my Json classes for all arrays that I should write in my database?
You're welcome, it's good practice to implement a toJson method on all your models that should be stored to the database. It makes it easier for you to refactor and update your model in the future.
Okay Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.