0

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: "Error: Invalid argument: Instance of 'MakeSentence'". I used getX state. I could only add two list with the below code:

LessonModel:

import 'make_sentence_model.dart';

class LessonModel {
  LessonModel({
    String? id,
    String? enWord,
    List<String>? wordMeaning,
    List<MakeSentence>? 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<String>() : [];
    if (json['make_sentence'] != null) {
      _makeSentence = [];
      json['make_sentence'].forEach((v) {
        _makeSentence?.add(MakeSentence.fromJson(v));
      });
    }
    _createdAt = json['createdAt'];
  }

  String? _id;
  String? _enWord;
  List<MakeSentence>? _makeSentence;
  String? _createdAt;

  String? get id => _id;
  String? get enWord => _enWord;
  List<MakeSentence>? get makeSentence => _makeSentence;
  String? get createdAt => _createdAt;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = _id;
    map['en_word'] = _enWord;
    if (_makeSentence != null) {
      map['make_sentence'] = _makeSentence?.map((v) => v.toJson()).toList();
    }
    map['createdAt'] = _createdAt;
    return map;
  }
}

MakeSentence:

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 => _nativeSentence;

  String? get enSentence => _enSentence;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['native_sentence'] = _nativeSentence;
    map['en_sentence'] = _enSentence;
    return map;
  }
}

HomeService:

class HomeService {
  final FirebaseFirestore _fireStore = FirebaseFirestore.instance;

  Future<LessonModel> saveLesson(LessonModel lesson) async {

    DocumentReference docRef = _fireStore
        .collection("save_word")
        .doc(lesson.userID)
        .collection("lesson_list")
        .doc();

    await docRef.set({
      "id": docRef.id,
      "en_word": lesson.enWord,
      "make_sentence": lesson.makeSentence,
      "createdAt": lesson.createdAt
    });
    return lesson;
  }
}

HomeController:

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<MakeSentence>? 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("Error: ${e.toString()}");
      Get.showSnackbar(
          const GetSnackBar(title: 'Error', message: 'something went wrong'));
    }
  }

}

HomePage:

class HomePage extends GetView<HomeController> {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<MakeSentence> makeSentence= [
      MakeSentence(enSentence: "English",nativeSentence: "Native")
    ];
    return Scaffold(
      backgroundColor: Color(0xFF47A1A0),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.saveLesson(
            enWord: "Slumber",
            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,
     
    );
  }
}
Ahmed Razeeb
  • 23
  • 1
  • 4

0 Answers0