title: How to add List of Object to Firestore In Flutter
body: <p>I need to add list of objects in firestore. When I save the item to firestore show errors but it doesn't save. This is the error Message: <em><strong>&quot;Error: Invalid argument: Instance of 'MakeSentence'&quot;</strong></em>. I used getX state. I could only add two list with the below code:</p>
  <p>LessonModel:</p>
  <pre><code>import 'make_sentence_model.dart';
  
  class LessonModel {
    LessonModel({
      String? id,
      String? enWord,
      List&lt;String&gt;? wordMeaning,
      List&lt;MakeSentence&gt;? makeSentence,
      String? createdAt,}) {
      _id = id;
      _enWord = enWord;
      _wordMeaning = wordMeaning;
      _makeSentence = makeSentence;
      _createdAt = createdAt;
    }
  
    LessonModel.fromJson(dynamic json) {
      _id = json['id'];
      _enWord = json['en_word'];
      _wordMeaning = json['word_meaning'] != null ? json['word_meaning'].cast&lt;String&gt;() : [];
      if (json['make_sentence'] != null) {
        _makeSentence = [];
        json['make_sentence'].forEach((v) {
          _makeSentence?.add(MakeSentence.fromJson(v));
        });
      }
      _createdAt = json['createdAt'];
    }
  
    String? _id;
    String? _enWord;
    List&lt;MakeSentence&gt;? _makeSentence;
    String? _createdAt;
  
    String? get id =&gt; _id;
    String? get enWord =&gt; _enWord;
    List&lt;MakeSentence&gt;? get makeSentence =&gt; _makeSentence;
    String? get createdAt =&gt; _createdAt;
  
    Map&lt;String, dynamic&gt; toJson() {
      final map = &lt;String, dynamic&gt;{};
      map['id'] = _id;
      map['en_word'] = _enWord;
      if (_makeSentence != null) {
        map['make_sentence'] = _makeSentence?.map((v) =&gt; v.toJson()).toList();
      }
      map['createdAt'] = _createdAt;
      return map;
    }
  }
  </code></pre>
  <p>MakeSentence:</p>
  <pre><code>class MakeSentence {
    MakeSentence({
      String? nativeSentence,
      String? enSentence,
    }) {
      _nativeSentence = nativeSentence;
      _enSentence = enSentence;
    }
  
    MakeSentence.fromJson(dynamic json) {
      _nativeSentence = json['native_sentence'];
      _enSentence = json['en_sentence'];
    }
  
    String? _nativeSentence;
    String? _enSentence;
  
    String? get nativeSentence =&gt; _nativeSentence;
  
    String? get enSentence =&gt; _enSentence;
  
    Map&lt;String, dynamic&gt; toJson() {
      final map = &lt;String, dynamic&gt;{};
      map['native_sentence'] = _nativeSentence;
      map['en_sentence'] = _enSentence;
      return map;
    }
  }
  </code></pre>
  <p>HomeService:</p>
  <pre><code>class HomeService {
    final FirebaseFirestore _fireStore = FirebaseFirestore.instance;
  
    Future&lt;LessonModel&gt; saveLesson(LessonModel lesson) async {
  
      DocumentReference docRef = _fireStore
          .collection(&quot;save_word&quot;)
          .doc(lesson.userID)
          .collection(&quot;lesson_list&quot;)
          .doc();
  
      await docRef.set({
        &quot;id&quot;: docRef.id,
        &quot;en_word&quot;: lesson.enWord,
        &quot;make_sentence&quot;: lesson.makeSentence,
        &quot;createdAt&quot;: lesson.createdAt
      });
      return lesson;
    }
  }
  </code></pre>
  <p>HomeController:</p>
  <pre><code>class HomeController extends GetxController {
    late User user;
    var lessonList = [].obs;
    final HomeService homeService = HomeService();
  
    @override
    void onInit() async {
      super.onInit();
      user = Get.arguments;
    }
  
    void saveLesson({
      String? enWord,
      List&lt;MakeSentence&gt;? makeSentence,
      String? createdAt,
    }) async {
      try {
  
        LessonModel lModel = LessonModel(
          enWord: enWord,
          makeSentence: makeSentence,
          createdAt: createdAt,
        );
  
        await homeService.saveLesson(lModel);
        Get.showSnackbar(
            const GetSnackBar(title: 'Success', message: 'Contact saved'));
      } catch (e) {
        debugPrint(&quot;Error: ${e.toString()}&quot;);
        Get.showSnackbar(
            const GetSnackBar(title: 'Error', message: 'something went wrong'));
      }
    }
  
  }
  </code></pre>
  <p>HomePage:</p>
  <pre><code>class HomePage extends GetView&lt;HomeController&gt; {
    const HomePage({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      List&lt;MakeSentence&gt; makeSentence= [
        MakeSentence(enSentence: &quot;English&quot;,nativeSentence: &quot;Native&quot;)
      ];
      return Scaffold(
        backgroundColor: Color(0xFF47A1A0),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            controller.saveLesson(
              enWord: &quot;Slumber&quot;,
              makeSentence: makeSentence,
              createdAt: DateTime.now().toString(),
            );
          },
          child: Icon(
            Icons.add,
            color: Color(0xFF47A1A0),
            size: 36,
          ),
          backgroundColor: Colors.white,
          tooltip: 'Add',
          elevation: 5,
          splashColor: Colors.white,
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
       
      );
    }
  }
  </code></pre>