Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB + Flutter

I am trying to create an app that uses AWS Services, I already use Cognito plugin for flutter but can't get it to work with DynamoDB, should I use a lambda function and point to it or is it possible to get data form a table directly from flutter, if that's the case which URL should I use?

I am new in AWS Services don’t know if is it possible to access a dynamo table with a URL or I should just use a lambda function

like image 240
Renato Avatar asked Nov 24 '25 13:11

Renato


2 Answers

Since this is kind of an open-ended question and you mentioned Lambdas, I would suggest checking out the Serverless framework. They have a couple of template applications in various languages/frameworks. Serverless makes it really easy to spin up Lambdas configured to an API Gateway, and you can start with the default proxy+ resource. You can also define DynamoDB tables to be auto-created/destroyed when you deploy/destroy your serverless application. When you successfully deploy using the command 'serverless deploy' it will output the URL to access your API Gateway which will trigger your Lambda seamlessly.

Then once you have a basic "hello-word" type API hosted on AWS, you can just follow the docs along for how to set up the DynamoDB library/sdk for your given framework/language.

Let me know if you have any questions!

-PS: I would also, later on, recommend using the API Gateway Authorizer against your Cognito User Pool, since you already have auth on the Flutter app, then all you have to do is pass through the token. The Authorizer can also be easily set up via the Serverless Framework! Then your API will be authenticated at the Gateway level, leaving AWS to do all the hard work :)

like image 152
Alan Negrete Avatar answered Nov 26 '25 08:11

Alan Negrete


If you want to read directly from Dynamo It is actually pretty easy.

  1. First add this package to your project.

  2. Then create your models you want to read and write. Along with conversion methods.

class Parent {
   String name;
   late List<Child> children;

 
  factory Parrent.fromDBValue(Map<String, AttributeValue> dbValue) {
    name = dbValue["name"]!.s!;
    children = dbValue["children"]!.l!.map((e) =>Child.fromDB(e)).toList();
  }


  Map<String, AttributeValue> toDBValue() {
    Map<String, AttributeValue> dbMap = Map();
    dbMap["name"] = AttributeValue(s: name);
    dbMap["children"] = AttributeValue(
        l: children.map((e) => AttributeValue(m: e.toDBValue())).toList());
    return dbMap;
  }
}

(AttributeValue comes from the package)

Then you can consume dynamo db api as per normal.

  1. Create Dynamo service
class DynamoService {
  final service = DynamoDB(
      region: 'af-south-1',
      credentials: AwsClientCredentials(
          accessKey: "someAccessKey",
          secretKey: "somesecretkey"));

  Future<List<Map<String, AttributeValue>>?> getAll(
      {required String tableName}) async {
    var reslut = await service.scan(tableName: tableName);
    return reslut.items;
  }

  Future insertNewItem(Map<String, AttributeValue> dbData, String tableName) async {
    service.putItem(item: dbData, tableName: tableName);
  }
}

Then you can convert when getting all data from dynamo.

List<Parent> getAllParents() {
   List<Map<String, AttributeValue>>? parents =
        await dynamoService.getAll(tableName: "parents");
  return parents!.map((e) =>Parent.fromDbValue(e)).toList()
}

You can check all Dynamo operations from here

like image 31
Terblanche Daniel Avatar answered Nov 26 '25 09:11

Terblanche Daniel