Untitled
plain_text
a month ago
5.1 kB
0
Indexable
Never
Future<void> csvData() async { FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: ['csv'], allowMultiple: false); if (result != null) { commonConfirmationDialog(context, DIALOG_TYPE_DELETE, () async { Navigator.pop(context); appStore.setLoading(true); var fileBytes = result.files.first.bytes; List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter().convert(String.fromCharCodes(fileBytes!), eol: "\n"); for (int i = 1; i < rowsAsListOfValues.length; i++) { ExercisesResponse mExerciseModel = ExercisesResponse(); mExerciseModel.name = rowsAsListOfValues.elementAt(i).elementAt(0).toString().validate(); mExerciseModel.description = rowsAsListOfValues.elementAt(i).elementAt(1).toString().validate(); mExerciseModel.thumbnail = rowsAsListOfValues.elementAt(i).elementAt(2).toString().validate(); mExerciseModel.video = rowsAsListOfValues.elementAt(i).elementAt(3).toString().validate(); mExerciseModel.level = rowsAsListOfValues.elementAt(i).elementAt(6).toString().validate(); mExerciseModel.sets = rowsAsListOfValues.elementAt(i).elementAt(7).toString().validate(); mExerciseModel.reps = rowsAsListOfValues.elementAt(i).elementAt(8).toString().validate(); mExerciseModel.createdAt = DateTime.now(); List<String> type = rowsAsListOfValues.elementAt(i).elementAt(4).toString().split(', '); List<String> typeId = []; List<String> bodyPart = rowsAsListOfValues.elementAt(i).elementAt(5).toString().split(', '); List<String> bodyPartId = []; if (type.isNotEmpty) { // type.forEach((element) async { for (int i = 0; i < type.length; i++) { await workoutCategoryService.isWorkoutExist(type[i]).then((value) async { print("value------>" + value.toString()); if (value == false) { WorkoutCategoryResponse mModel = WorkoutCategoryResponse(); mModel.title = type[i]; mModel.thumbnail = ""; mModel.subTitle = ""; mModel.isDashboard = false; mModel.type = ""; mModel.createdAt = DateTime.now(); await workoutCategoryService.addDocument(mModel.toJson()).then((value) { setState(() { typeId.add(value.id.validate()); }); print("Save"); }).catchError((e) { toast(e.toString()); }); } else { await workoutCategoryService.getWorkoutId(type[i]).then((value) { setState(() { typeId.add(value.validate()); }); }); } }); } } for (int i = 0; i < bodyPart.length; i++) { await bodyPartService.isBodyPartExist(bodyPart[i]).then((value) async { print("value------>" + value.toString()); if (value == false) { BodyPartResponse mBodyPartModel = BodyPartResponse(); mBodyPartModel.name = bodyPart[i].toString(); mBodyPartModel.image = ""; mBodyPartModel.isDashboard = false; mBodyPartModel.createdAt = DateTime.now(); await bodyPartService.addDocument(mBodyPartModel.toJson()).then((value) { print("done"); setState(() { bodyPartId.add(value.id.validate()); }); setState(() {}); }).catchError((e) { toast(e.toString()); }); } else { await bodyPartService.getBodyPartId(bodyPart[i]).then((value) { setState(() { print("step1" + value.validate()); bodyPartId.add(value.validate()); }); }).catchError((e) { print("error--->" + e.toString()); }); } }); } mExerciseModel.workoutType = typeId; mExerciseModel.bodyParts = bodyPartId; print("error final--->" + mExerciseModel.toJson().toString()); await exercisesService.isExercisesExist(mExerciseModel.name).then((value) async { if (value == false) { await exercisesService.addDocument(mExerciseModel.toJson()).then((value) { toast("value"); searchList.clear(); mExerciseList.clear(); loadExerciseData(); setState(() {}); }).catchError((e) { toast(e.toString()); }); } }); } appStore.setLoading(false); setState(() {}); }, title: "Upload CSV File", subtitle: "Are you sure you want upload csv file?"); } }