This widget help you to mock backend responses in flutter project.
The widget was only tested on following environment,
- Flutter: 3.7.5+ (with sound null safety)
- Dio: 5.0.0+
-
Install:
dev_dependencies: dio_mock_interceptor: ^1.4.0
-
Create a
mock
folder in your project, add json files to mock http responses, example: /mock/common.json[ { "path": "/api/login", "method": "POST", "statusCode": 200, "data": { "success": true, "code": "0000", "result": { "test": "test" } } }, { "path": "/api/logout", "method": "POST", "statusCode": 200, "data": {} } ]
-
Setup
mock
folder toassets
section ofpubspec.yaml
:assets: - mock/
-
Add interceptor to Dio:
import 'package:dio_mock_interceptor/dio_mock_interceptor.dart'; dio.interceptors.add(MockInterceptor());
-
Dio post example:
Response response = await dio.post("/api/login"); String json = response.data; if (json.isEmpty) { throw Exception('response is empty'); } Map<String, dynamic> data = jsonDecode(json); bool isSuccess = data['success'] as bool; // true Map<String, dynamic> result = data['result']; // result['test'] = 'test'
-
EL/req example:
[ { "path": "/api/data/req-param", "method": "POST", "statusCode": 200, "data": { "id": "yong-xin", "desc": "Hi, ${req.data.name}, I am ${req.data.name2}" } } ]
-
Template example:
[ { "path": "/api/template/list", "method": "POST", "statusCode": 200, "template": { "size": 100000, "content": { "id": "test${index}", "name": "name_${index}" } } }, { "path": "/api/data/template", "method": "POST", "statusCode": 200, "data": { "id": "yong-xin", "listA": "${template}" }, "template": { "size": 1000, "content": { "id": "test${index}", "name": "name_${index}" } } }, { "path": "/api/data/template2", "method": "POST", "statusCode": 200, "data": { "id": "yong-xin", "listA": "${template}", "field2": { "listB": "${template}" } }, "template": { "size": 1000, "content": { "id": "test${index}", "name": "name_${index}" } } }, { "path": "/api/data/templates", "method": "POST", "statusCode": 200, "data": { "id": "yong-xin", "listA": "${templates.name1}", "field": { "listB": "${templates.name2}" } }, "templates": { "name1": { "size": 1000, "content": { "id": "test${index}", "name": "name_${index}" } }, "name2": { "size": 10, "content": { "id": "test2${index}", "name": "name2_${index}" } } } } ]
-
Vars example:
[ { "path": "/api/data/vars", "method": "POST", "statusCode": 200, "data": { "id": "yong-xin", "listA": "${templates.name1}", "field": { "listB": "${templates.name2}" }, "arry": "${groups}", "objA": "${obj}" }, "vars": { "n": 5, "groups": [ "May", "YongXin", "John" ], "obj": { "name": "objName" } }, "templates": { "name1": { "size": 1000, "content": { "id": "test${index}", "group": "g_${groups.elementAt(index%3)}", "name": "name_${index}" } }, "name2": { "size": 10, "content": { "id": "test2${index}", "name": "name2_${index}" } } } } ]
Copyright (c) 2023-present Yong-Xin Technology Ltd.
This project is licensed under the MIT License - see the LICENSE file for details.