如何从我的dynamoDB中检索数据并将它们显示在我的颤振应用程序中?
我试过在网上看很多东西,但找不到什么可以尝试的东西。
它是关于显示存储在表中的温度传感器数据。
有人能提点建议吗?
谢谢。
发布于 2022-09-16 17:26:16
其实很容易。
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。
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);
}
}然后,当从发电机获取所有数据时,您可以进行转换。
List<Parent> getAllParents() {
List<Map<String, AttributeValue>>? parents =
await dynamoService.getAll(tableName: "parents");
return parents!.map((e) =>Parent.fromDbValue(e)).toList()
}您可以检查这里中的所有Dynamo操作
https://stackoverflow.com/questions/66835605
复制相似问题