>,dart"/>

首页 > 解决方案 > 转换列表到地图>

问题描述

我有这段代码按预期工作:

import 'package:postgres/postgres.dart';

main(List<String> arguments) async {

 String host = "localhost";
 int port = 5432;
 String dbName = "quakedata";
 String username = "postgres";
 String password = "xxx";

 var connection;

 try {
  connection = PostgreSQLConnection(host, port, dbName, username: username, password: password);
  await connection.open();

  var query = """select geojson from dm_quakefeatures order by modified_date desc limit 10""";

  List<List<dynamic>> results = await connection.query(query);

  for(final row in results){
    for(final thisIsAMap in row){
      print(thisIsAMap);
    }
  }

 } catch (exception, stackTrace) {
   print(exception);
   print(stackTrace);
 } finally {
   connection.close();
 }
}

我困惑的是这两点:

  1. 根据 postgres 包文档,方法query()应该返回 Future<List<List>>但返回List<List<dynamic>>?为什么?这是因为await?
  2. 我知道在thisIsAMap变量中我将接收这种格式 { "properties": { "mag": 1.4, "place": "Southern Alaska", "time": 1544108572101, "updated": 1544108744865, "tsunami": 0, "type": "earthquake", "title": "M 1.4 - Southern Alaska" }, "geometry": { "type": "Point", "coordinates": [ -149.9893, 61.4013, 32.8 ] } } 的数据:这将完全适合Map<String, Map<String, dynamic>>数据类型。我怎样才能把它转换成它?

标签: dart

解决方案


推荐阅读