Json 构建用于同步数据的API

Json 构建用于同步数据的API,json,laravel,flutter,Json,Laravel,Flutter,我正在Flitter中构建一个应用程序,使工程师能够在现场完成检查,然后稍后将检查同步回后端服务器(Laravel)。 检查由存储在sqlite数据库中的设备上的检查组成。每个检查都有多个答案,这些答案也存储在sqlite数据库中,每个记录都使用检查id字段引用检查 检查记录具有“已完成”和“已同步”字段,因此在同步时,我检索所有已完成和未同步的检查,然后检索相关的答案。这就给了我一个列表,其中包含一个用于检查的地图,以及一个单独的列表,其中包含多个用于回答的地图 List<Map<

我正在Flitter中构建一个应用程序,使工程师能够在现场完成检查,然后稍后将检查同步回后端服务器(Laravel)。 检查由存储在sqlite数据库中的设备上的检查组成。每个检查都有多个答案,这些答案也存储在sqlite数据库中,每个记录都使用检查id字段引用检查

检查记录具有“已完成”和“已同步”字段,因此在同步时,我检索所有已完成和未同步的检查,然后检索相关的答案。这就给了我一个列表,其中包含一个用于检查的地图,以及一个单独的列表,其中包含多个用于回答的地图

List<Map<String, dynamic>> inspections = [{"id":1,"client_id":1,"job_no":1111,"site":"Somewhere","completed":1,"synced":null,"checked_on":"2020-11-03 11:37:54.751734"}];

List<Map<String, dynamic>> answers = [{id: 1, inspection_id: 1, question_id: 1, remedial_type: Acceptable, comment: Save, picture_filename: null, picture_url: null}, {id: 2, inspection_id: 1, question_id: 4, remedial_type: Acceptable, comment: , picture_filename: null, picture_url: null}];
由于我无法在laravel端操作数据对象,因此我可能离所需的目标很远

尝试

但是没有快乐。得到 非法字符串偏移量“检查”

前进一步,后退两步

更新2

我已经成功地构建了一个json对象,在Laravel端对该对象进行解码,将该对象添加到服务器数据库,并返回一个响应,列出已同步的检查ID,这样Flatter应用程序就可以将其标记为已同步

Future<void> sendInspections(context, scaffoldKey) async {
    List<Map<String, dynamic>> inspections = [];
    Map<String, dynamic> sendData = {};
    var body;

    await Provider.of<Inspections>(context, listen: false)
        .getCompletedInspections();
    List<Inspection> _completedInspections =
        Provider.of<Inspections>(context, listen: false).completedInspections;
    if (_completedInspections.length < 1) {
      showMsg("There are no completed Inspections to sync!", scaffoldKey);
    } else {
      for (Inspection inspection in _completedInspections) {
        Map<String, dynamic> inspectionData = {};
        inspectionData.addAll(inspection.toMap());

        await Provider.of<Answers>(context, listen: false)
            .fetchAnswersByInspectionId(inspection.id);
        List<Answer> _completedAnswers =
            Provider.of<Answers>(context, listen: false).completedAnswers;

        List<Map<String, dynamic>> innerData = [];
        for (Answer answer in _completedAnswers) {
          innerData.add(answer.toMap());
        }
        inspectionData['answers'] = innerData;
        inspections.add(inspectionData);
      }
      sendData['inspection'] = inspections;
      print("sendData = ${jsonEncode(sendData)}");
      try {
        body = await Network()
            .sendDataToServer(sendData, scaffoldKey, 'inspections');
        print("Worked body = ${body['synced_inspection_ids'].length}");
        InspectionDBHelper inspectionDBHelper = InspectionDBHelper();
        print(body['synced_inspection_ids']);
        for (int id in body['synced_inspection_ids']) {
          print(id);
          Inspection inspection =
              await inspectionDBHelper.getInspectionById(id);
          inspection.synced = 1;
          inspectionDBHelper.update(inspection);
          if (body['synced_inspection_ids'].length > 0) {
            showMsg(
                "${body['synced_inspection_ids'].length} inspections were successfully synced to the EPS Inspect server.",
                scaffoldKey);
          } else {
            showMsg("No Inspections were synced!", scaffoldKey);
          }
        }
      } catch (e) {
        showMsg("There was an error while syncing your inspections! : $e",
            scaffoldKey);
      }
    }
  }
我曾经检查构建的json对象是否有效,然后在Laravel端使用此代码循环json对象并检索信息

public function inspections(Request $request){
        $body = $request->getContent();
        $data = json_decode($body, true);
        $ids_synced = [];
        foreach($data['inspection'] as $inspection){
            $new_inspection = new Inspection;
            $new_inspection->client_id = $inspection['client_id'];
            $new_inspection->job_number = $inspection['job_no'];
            $new_inspection->site = $inspection['site'];
            $new_inspection->checked_on = $inspection['checked_on'];
            $new_inspection->save();
            array_push($ids_synced, $inspection['id']);
            foreach($inspection['answers'] as $answer){
                $new_answer = new Answer;
                $new_answer->inspection_id = $new_inspection->id;
                $new_answer->question_id = $answer['question_id'];
                $new_answer->remedial_type = $answer['remedial_type'];
                $new_answer->comment = $answer['comment'];
                $new_answer->save();
            }
        }
        return response()->json(['message' => "All completed inspections have synced with EPS Inspect server!!", 'synced_inspection_ids' => $ids_synced]);
    }
不确定这是否是最佳实践,但它解决了我的问题

我将等待几天,看看这是否是一个可行的解决方案,然后将其标记为已回答

sendData = [{"inspection":{"id":1,"client_id":1,"job_no":1111,"site":"Swansea","completed":1,"synced":null,"checked_on":"2020-11-03 11:37:54.751734","answers":[{"id":1,"inspection_id":1,"question_id":1,"remedial_type":"Acceptable","comment":"Save","picture_filename":null,"picture_url":null},{"id":2,"inspection_id":1,"question_id":4,"remedial_type":"Acceptable","comment":"","picture_filename":null,"picture_url":null}]}},{"inspection":{"id":2,"client_id":2,"job_no":2222,"site":"Cardiff","completed":1,"synced":null,"checked_on":"2020-11-04 10:40:53.211043","answers":[{"id":3,"inspection_id":2,"question_id":1,"remedial_type":"Acceptable","comment":"","picture_filename":null,"picture_url":null},{"id":4,"inspection_id":2,"question_id":3,"remedial_type":"Acceptable","comment":"","picture_filename":null,"picture_url":null}]}}]
$data = $request->getContent();
echo($data[0]['inspection']);
Future<void> sendInspections(context, scaffoldKey) async {
    List<Map<String, dynamic>> inspections = [];
    Map<String, dynamic> sendData = {};
    var body;

    await Provider.of<Inspections>(context, listen: false)
        .getCompletedInspections();
    List<Inspection> _completedInspections =
        Provider.of<Inspections>(context, listen: false).completedInspections;
    if (_completedInspections.length < 1) {
      showMsg("There are no completed Inspections to sync!", scaffoldKey);
    } else {
      for (Inspection inspection in _completedInspections) {
        Map<String, dynamic> inspectionData = {};
        inspectionData.addAll(inspection.toMap());

        await Provider.of<Answers>(context, listen: false)
            .fetchAnswersByInspectionId(inspection.id);
        List<Answer> _completedAnswers =
            Provider.of<Answers>(context, listen: false).completedAnswers;

        List<Map<String, dynamic>> innerData = [];
        for (Answer answer in _completedAnswers) {
          innerData.add(answer.toMap());
        }
        inspectionData['answers'] = innerData;
        inspections.add(inspectionData);
      }
      sendData['inspection'] = inspections;
      print("sendData = ${jsonEncode(sendData)}");
      try {
        body = await Network()
            .sendDataToServer(sendData, scaffoldKey, 'inspections');
        print("Worked body = ${body['synced_inspection_ids'].length}");
        InspectionDBHelper inspectionDBHelper = InspectionDBHelper();
        print(body['synced_inspection_ids']);
        for (int id in body['synced_inspection_ids']) {
          print(id);
          Inspection inspection =
              await inspectionDBHelper.getInspectionById(id);
          inspection.synced = 1;
          inspectionDBHelper.update(inspection);
          if (body['synced_inspection_ids'].length > 0) {
            showMsg(
                "${body['synced_inspection_ids'].length} inspections were successfully synced to the EPS Inspect server.",
                scaffoldKey);
          } else {
            showMsg("No Inspections were synced!", scaffoldKey);
          }
        }
      } catch (e) {
        showMsg("There was an error while syncing your inspections! : $e",
            scaffoldKey);
      }
    }
  }
{
    "inspection": [{
        "id": 1,
        "client_id": 1,
        "job_no": 1111,
        "site": "Swansea",
        "completed": 1,
        "synced": null,
        "checked_on": "2020-11-03 11:37:54.751734",
        "answers": [{
            "id": 1,
            "inspection_id": 1,
            "question_id": 1,
            "remedial_type": "Acceptable",
            "comment": "Save",
            "picture_filename": null,
            "picture_url": null
        }, {
            "id": 2,
            "inspection_id": 1,
            "question_id": 4,
            "remedial_type": "Acceptable",
            "comment": "",
            "picture_filename": null,
            "picture_url": null
        }]
    }, {
        "id": 2,
        "client_id": 2,
        "job_no": 2222,
        "site": "Cardiff",
        "completed": 1,
        "synced": null,
        "checked_on": "2020-11-04 10:40:53.211043",
        "answers": [{
            "id": 3,
            "inspection_id": 2,
            "question_id": 1,
            "remedial_type": "Acceptable",
            "comment": "",
            "picture_filename": null,
            "picture_url": null
        }, {
            "id": 4,
            "inspection_id": 2,
            "question_id": 3,
            "remedial_type": "Acceptable",
            "comment": "",
            "picture_filename": null,
            "picture_url": null
        }]
    }]
}
public function inspections(Request $request){
        $body = $request->getContent();
        $data = json_decode($body, true);
        $ids_synced = [];
        foreach($data['inspection'] as $inspection){
            $new_inspection = new Inspection;
            $new_inspection->client_id = $inspection['client_id'];
            $new_inspection->job_number = $inspection['job_no'];
            $new_inspection->site = $inspection['site'];
            $new_inspection->checked_on = $inspection['checked_on'];
            $new_inspection->save();
            array_push($ids_synced, $inspection['id']);
            foreach($inspection['answers'] as $answer){
                $new_answer = new Answer;
                $new_answer->inspection_id = $new_inspection->id;
                $new_answer->question_id = $answer['question_id'];
                $new_answer->remedial_type = $answer['remedial_type'];
                $new_answer->comment = $answer['comment'];
                $new_answer->save();
            }
        }
        return response()->json(['message' => "All completed inspections have synced with EPS Inspect server!!", 'synced_inspection_ids' => $ids_synced]);
    }