嗨,我正在构建一个应用程序,并尝试从api回调中解析嵌套的对象数组并收到此错误
类型字符串不是Map的子类型<字符串,动态>
这是模型课
/*function call :
select * from fn_return_pivot() ;
*/
create or replace function fn_return_pivot()
returns table(srno bigint , D text, N text , X text , F text, C text)
as
$$
declare v_query text ;
begin
v_query = (SELECT T.sql FROM (SELECT 'SELECT *
FROM crosstab(
$ct$SELECT u.attnum, t.rn, u.val
FROM (SELECT row_number() OVER () AS rn, * FROM '
|| attrelid::regclass || ') t
, unnest(ARRAY[' || string_agg(quote_ident(attname)
|| '::text', ',') || '])
WITH ORDINALITY u(val, attnum)
ORDER BY 1, 2$ct$
) t (attnum bigint, '
|| (SELECT string_agg('r'|| rn ||' text', ', ')
FROM (SELECT row_number() OVER () AS rn FROM "COUNT SHIFTS") t)
|| ')' AS sql
FROM pg_attribute
WHERE attrelid = '"COUNT SHIFTS"'::regclass
AND attnum > 0
AND NOT attisdropped
GROUP BY attrelid) AS T );
return query execute v_query ;
end ;
$$
language plpgsql ;
这是我收到的json回调
class Tournament {
String id;
String title;
String roomID;
String roomPass;
String map;
String type;
String date;
String time;
int joined;
String createdBy;
List<UserIds> joinedUsers;
Tournament(
{this.createdBy,
this.joinedUsers,
this.id,
this.date,
this.map,
this.roomID,
this.roomPass,
this.time,
this.title,
this.type,
this.joined});
factory Tournament.fromJson(Map<String, dynamic> json) {
var list = json['joinedUsers'] as List;
List<UserIds> userList =
list.map((data) => UserIds.fromJson(data)).toList();
return Tournament(
id: json['_id'],
title: json['title'],
roomID: json['roomId'],
roomPass: json['roomPass'],
map: json['mapType'],
type: json['type'],
date: json['date'],
time: json['time'],
joined: json['joined'],
createdBy: json['createdBy'],
joinedUsers: userList);
}
}
class UserIds {
String userId;
UserIds({this.userId});
factory UserIds.fromJson(Map<String, dynamic> parsedJson) {
return UserIds(userId: parsedJson['\$oid']);
}
}
现在,当我运行它时,它给了我一个错误,提示类型String不是Map
答案 0 :(得分:0)
您可以尝试:
list.map((data) => UserIds.fromJson(JSON.parse(data)).toList();
可能是解析问题,您可以通过此操作先将元素记录到lambda中,以验证其提供的内容:
list.map((element) =>
{
console.log(data);
//UserIds.fromJson(JSON.parse(data)).toList();
});
答案 1 :(得分:0)
只需检查一下我为userId制作的示例: 我已经在本地获取了您的json。
import 'dart:convert';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class UserIds {
String userId;
UserIds({this.userId});
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool _isLoading = false;
List<UserIds> userIdsList = List();
@override
void initState() {
super.initState();
getData();
}
getData() async {
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
Map jsonData = json.decode(data);
jsonData['joinedUsers'].forEach((item) {
item.forEach((key, value) {
print('This is the Key $key value $value');
userIdsList.add(UserIds(userId: value));
});
});
print('This is the list ${userIdsList.length}');
}
@override
Widget build(BuildContext context) {
return Scaffold(body: Text(''));
}
}