flutterairtable

How to read and write Data in Airtable with Flutter?


dependencies: airtable: ^0.0.2 import 'package:airtable/airtable.dart'; ??

import 'package: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.


Solution

  • 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.

    In your example code you use the import statement for the first package but the code from the second one. As the first one doesn't provide a valid link to a repository I'll have a look at the second one.

    To retrieve records for a project from the API you can use this code:

    var records = await airtable.getAllRecords(recordName);
    

    Unfortunately the package has some bugs. As I wanted to create a record I used the method createRecord like this:

    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. 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. In a toJson method of the AirtableRecord model, the author added the unnecessary fields id and 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. Hopefully this helps you.

    Edit:

    I'll checked the repository again, and it seems that the author is inactive. 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.

    Here an simple example of how to do this (with Dio for the network connection):

    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. I definetly recommend to also use models for your response (have a look at this).

    I used one of their example workspaces. The projectBase was an ID. You can just look at their API documentation to see how to build the requests.

    Edit 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. And your own implementation with proper error handling.

    Edit 3:

    You can use the filterByFormula parameter to retrieve all the data that matches your condition (Reference).

    E.g.:

    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',
          },
        ),
      );