Dart 入门
本集成指南遵循 快速入门指南,并假设您已完全完成“动手”路径。您应该能够通过浏览 URL http://localhost:1337/api/restaurants 来使用该 API。
如果您尚未阅读快速入门指南,则使用 Dart 请求 Strapi API 的方式保持不变,只是您不会获取相同的内容。
创建 Dart 文件
确保您的计算机上已安装 Dart。
take dart-app
touch main.dart
使用 HTTP 客户端
我们将使用 http 发出 HTTP 请求。
- 创建一个包含以下内容的
pubspec.yaml
文件:
name: my_app
dependencies:
http: ^0.12.2
- 运行以下命令安装依赖项:
dart pub get
GET 请求您的集合类型
对 restaurant
集合类型执行 GET
请求以获取所有餐厅。
确保您已为 restaurant
集合类型激活 find
权限。
Map<String,String> headers = {
'Content-Type':'application/json',
'Accept': 'application/json'
};
var response = await http.get(
'http://localhost:1337/restaurants',
headers: headers
);
print(response.body)
[
{
"id": 1,
"name": "Biscotte Restaurant",
"description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.",
"created_by": {
"id": 1,
"firstname": "Paul",
"lastname": "Bocuse",
"username": null
},
"updated_by": {
"id": 1,
"firstname": "Paul",
"lastname": "Bocuse",
"username": null
},
"created_at": "2020-07-31T11:37:16.964Z",
"updated_at": "2020-07-31T11:37:16.975Z",
"categories": [
{
"id": 1,
"name": "French Food",
"created_by": 1,
"updated_by": 1,
"created_at": "2020-07-31T11:36:23.164Z",
"updated_at": "2020-07-31T11:36:23.172Z"
}
]
}
]
Example
import 'package:http/http.dart' as http;
import 'dart:convert';
class Restaurant {
static String api_url = 'http://localhost:1337';
static Map<String,String> headers = {
'Content-Type':'application/json',
'Accept': 'application/json'
};
void all() async {
var response = await http.get('${api_url}/restaurants', headers: headers);
print(response.body);
}
}
void main() {
var restaurant = Restaurant();
restaurant.all();
}
POST Request your collection type
Execute a POST
request on the restaurant
collection type in order to create a restaurant.
Be sure that you activated the create
permission for the restaurant
collection type and the find
permission for the category
Collection type.
In this example a japanese
category has been created which has the id: 3.
Map<String,String> headers = {
'Content-Type':'application/json',
'Accept': 'application/json'
};
var response = await http.post(
'http://localhost:1337/restaurants',
headers: headers,
body: jsonEncode({
'name': 'Dolemon Sushi',
'description': 'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious.',
'categories': [3]
});
);
{
"id": 2,
"name": "Dolemon Sushi",
"description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious",
"created_by": null,
"updated_by": null,
"created_at": "2020-08-04T09:57:11.669Z",
"updated_at": "2020-08-04T09:57:11.669Z",
"categories": [
{
"id": 3,
"name": "Japanese",
"created_by": 1,
"updated_by": 1,
"created_at": "2020-07-31T11:36:23.164Z",
"updated_at": "2020-07-31T11:36:23.172Z"
}
]
}
Example
import 'package:http/http.dart' as http;
import 'dart:convert';
class Restaurant {
static String api_url = 'http://localhost:1337';
static Map<String,String> headers = {
'Content-Type':'application/json',
'Accept': 'application/json'
};
void all() async {
var response = await http.get('${api_url}/restaurants', headers: headers);
print(response.body);
}
void create(name, description, category) async {
final data = jsonEncode({
'name': name,
'description': description,
'categories': category
});
var response = await http.post(api_url, headers: headers, body: data);
}
}
void main() {
var restaurant = Restaurant();
restaurant.create(
'Dolemon Sushi',
'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious.',
[3]
);
}
PUT Request your collection type
Execute a PUT
request on the restaurant
collection type in order to update the category of a restaurant.
Be sure that you activated the put
permission for the restaurant
collection type.
Map<String,String> headers = {
'Content-Type':'application/json',
'Accept': 'application/json'
};
var response = await http.put(
'http://localhost:1337/restaurants/2',
headers: headers,
body: jsonEncode({
'categories': [2]
});
);
{
"id": 2,
"name": "Dolemon Sushi",
"description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious",
"created_by": null,
"updated_by": null,
"created_at": "2020-08-04T10:21:30.219Z",
"updated_at": "2020-08-04T10:21:30.219Z",
"categories": [
{
"id": 2,
"name": "Brunch",
"created_by": 1,
"updated_by": 1,
"created_at": "2020-08-04T10:24:26.901Z",
"updated_at": "2020-08-04T10:24:26.911Z"
}
]
}
Example
import 'package:http/http.dart' as http;
import 'dart:convert';
class Restaurant {
static String api_url = 'http://localhost:1337';
static Map<String,String> headers = {
'Content-Type':'application/json',
'Accept': 'application/json'
};
void all() async {
var response = await http.get('${api_url}/restaurants', headers: headers);
print(response.body);
}
void create(name, description, category) async {
final data = jsonEncode({
'name': name,
'description': description,
'categories': category
});
var response = await http.post('${api_url}/restaurants', headers: headers, body: data);
}
void update(id, params) async {
final data = jsonEncode({
'categories': params['categories']
});
var response = await http.put("${api_url}/restaurants/${id}", headers: headers, body: data);
}
}
void main() {
var restaurant = Restaurant();
restaurant.update(2, {'categories': [2]});
}