首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检索我的dynamoDB数据到颤振应用程序

检索我的dynamoDB数据到颤振应用程序
EN

Stack Overflow用户
提问于 2021-03-27 20:06:01
回答 1查看 445关注 0票数 1

如何从我的dynamoDB中检索数据并将它们显示在我的颤振应用程序中?

我试过在网上看很多东西,但找不到什么可以尝试的东西。

它是关于显示存储在表中的温度传感器数据。

有人能提点建议吗?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-09-16 17:26:16

其实很容易。

  1. 首先,将包添加到项目中。
  2. 然后创建你想要读和写的模型。以及转换方法。
代码语言:javascript
复制
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来自于包)

然后,您可以按照正常情况使用dynamo。

  1. 创建Dynamo服务
代码语言:javascript
复制
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);
  }
}

然后,当从发电机获取所有数据时,您可以进行转换。

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

您可以检查这里中的所有Dynamo操作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66835605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档