[英]How to read and write Data in Airtable with Flutter?
dependencies: airtable: ^0.0.2 import 'package:airtable/airtable.dart';依赖项:airtable: ^0.0.2 import 'package:airtable/airtable.dart'; ??
??
import 'package:dart_airtable/dart_airtable.dart';导入'包:dart_airtable/dart_airtable.dart'; ??
??
void main() async {
final apiKey = 'my-airtable-api-key'
final projectBase = 'my-airtable-project-base';
final recordName = 'Tasks';
var airtable = Airtable(apiKey: apiKey, projectBase: projectBase);
var records = await airtable.getAllRecords(recordName);
print(records);
}
If anyone knows how to solve it, I will be very grateful.如果有人知道如何解决它,我将不胜感激。
First of all, it seems like you mix two things together.首先,您似乎将两件事混合在一起。 Currently there are two
packages
that provide a library for the communication with airtable: airtable 0.0.2 and dart_airtable 0.1.1 .目前有两个
packages
提供了与 airtable 通信的库: airtable 0.0.2和dart_airtable 0.1.1 。
In your example code you use the import statement for the first package
but the code from the second one.在您的示例代码中,您使用第一个
package
的 import 语句,但使用第二个package
的代码。 As the first one doesn't provide a valid link to a repository
I'll have a look at the second one.由于第一个没有提供到
repository
的有效链接,我将看看第二个。
To retrieve records
for a project from the API you can use this code:要从 API 检索项目的
records
,您可以使用以下代码:
var records = await airtable.getAllRecords(recordName);
Unfortunately the package
has some bugs.不幸的是,该
package
有一些错误。 As I wanted to create a record I used the method createRecord
like this:因为我想创建一个记录,所以我使用了
createRecord
方法,如下所示:
var test = await airtable.createRecord(
recordName,
AirtableRecord(
fields: [
AirtableRecordField(
fieldName: 'Short Description',
value: 'Test Description',
),
],
),
);
print(test);
But the only response
I got was null
.但我得到的唯一
response
是null
。 So I cloned the project and debugged the method.所以我克隆了项目并调试了方法。 The result was this:
结果是这样的:
{"error":{"type":"INVALID_REQUEST_UNKNOWN","message":"Invalid request: parameter validation failed. Check your request data."}}
After a little bit of searching I found out that the package
sends wrong data to the airtable API.经过一番搜索,我发现该
package
向 airtable API 发送了错误的数据。 In a toJson
method of the AirtableRecord
model, the author added the unnecessary fields id
and createdTime
.在
AirtableRecord
模型的一个toJson
方法中,作者添加了不必要的字段id
和createdTime
。 After deleting them, the method works.删除它们后,该方法有效。
Response:回复:
AirtableRecord(id: rec9bqN78Le1dbC1g, createdTime: 2020-10-20 19:10:21.000Z, fields: {Short Description: Test Description})
I didn't look for the remaining methods, but my advice is to write your own connection to the airtime API or use the existing package
and change the things that aren't working.我没有寻找剩余的方法,但我的建议是编写自己的与 airtime API 的连接或使用现有
package
并更改不起作用的东西。 Hopefully this helps you.希望这对你有帮助。
Edit:编辑:
I'll checked the repository
again, and it seems that the author is inactive.我再去查一下
repository
,貌似作者不活跃。 As it only contains a few methods and models to work with the airtable API, I'll guess it's not a bad idea to just wrote your own implementation.由于它只包含一些与 airtable API 一起使用的方法和模型,我猜想自己编写实现并不是一个坏主意。
Here an simple example of how to do this (with Dio for the network connection):这是一个如何执行此操作的简单示例(使用Dio进行网络连接):
try {
final response = await Dio().post(
'https://api.airtable.com/v0/$projectBase/$recordName',
options: Options(
contentType: 'Application/json',
headers: {
'Authorization': 'Bearer $yourApiKey',
'Accept': 'Application/json',
},
),
data: {
'records': [
{
'fields': {
'Short Description': 'Cactus',
'Total': 11.5,
}
},
],
},
);
// TODO: Whatever you want to do with the response. A good practice is to transform it into models and than work with them
print(response);
} on DioError catch (e) {
// TODO: Error handling
if (e.response != null) {
print(e.response.data);
} else {
print(e.request);
print(e.message);
}
}
Response:回复:
{"records":[{"id":"recrvxH93gJgAGo7j","fields":{"Short Description":"Cactus","Total":11.5},"createdTime":"2020-10-21T20:41:19.000Z"}]}
You could now extend this, maybe as a service
with the GetIt
package and add your required funtions.你现在可以扩展它,也许作为一个带有
GetIt
包的service
并添加你需要的功能。 I definetly recommend to also use models
for your response
(have a look at this ).我明确建议您也使用
models
来response
(看看这个)。
I used one of their example workspaces.我使用了他们的示例工作区之一。 The projectBase was an ID.
projectBase 是一个 ID。 You can just look at their API documentation to see how to build the
requests
.您可以查看他们的 API 文档以了解如何构建
requests
。
Edit 2:编辑2:
Read could be simple done by this:阅读可以通过以下方式简单地完成:
final response = await Dio().get(
'https://api.airtable.com/v0/$projectBase/$recordName',
options: Options(
contentType: 'Application/json',
headers: {
'Authorization': 'Bearer $yourApiKey',
'Accept': 'Application/json',
},
),
);
Update:更新:
final response = await Dio().patch(
'https://api.airtable.com/v0/$projectBase/$recordName',
options: Options(
contentType: 'Application/json',
headers: {
'Authorization': 'Bearer $yourApiKey',
'Accept': 'Application/json',
},
),
data: {
'records': [
{
'id': 'rechYkD0pDW1NjFAF',
'fields': {
'Short Description': 'Cactus II',
'Total': 11.5,
}
},
],
},
);
And delete:并删除:
final response = await Dio().delete(
'https://api.airtable.com/v0/$projectBase/$recordName/rec0bMv507juqS7pv',
options: Options(
headers: {
'Authorization': 'Bearer $yourApiKey',
'Accept': 'Application/json',
},
),
);
These are only examples, I advice you to also read the API documentation to get a overview of what is possible.这些只是示例,我建议您也阅读 API 文档以了解可能的内容。 And your own implementation with proper error handling.
以及您自己的正确错误处理的实现。
Edit 3:编辑3:
You can use the filterByFormula
parameter to retrieve all the data that matches your condition ( Reference ).您可以使用
filterByFormula
参数来检索与您的条件匹配的所有数据 ( Reference )。
Eg:例如:
final response = await Dio().get(
'https://api.airtable.com/v0/$projectBase/$recordName',
queryParameters: {
'filterByFormula': 'SEARCH("Cactus",{Short Description})' // Searches the value 'Cactus' in the 'Short description' field.
},
options: Options(
headers: {
'Authorization': 'Bearer $yourApiKey',
'Accept': 'Application/json',
},
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.