To perform a POST action on a web service, follow these steps :
- Log into the Mock Lab service at https://app.mocklab.io/ and click on the Stubs section of the example API. Then, create a new Stub.
- Complete the request, as follows:
- Name: Post Pizza
- Verb: POST
- Address: /pizza
- Status: 201
- Body: {"message": "The pizza was posted"}:
- Press the Save button.
- In the Flutter project, in the httpHelper.dart file, in the HttpHelper class, create a new method called postPizza, as follows:
Future<String> postPizza(Pizza pizza) async {
String post = json.encode(pizza.toJson());
Uri url = Uri.https(authority, postPath);
http.Response r = await http.post(
url,
body: post,
);
return r.body;
}
- In the project, create a new file called pizza_detail.dart.
- At the top of the new file, add the required imports:
import 'package:flutter/material.dart';
import 'pizza.dart';
import 'httphelper.dart';