Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何解析颤振中的graphql响应_Json_Parsing_Flutter_Dart_Graphql - Fatal编程技术网

Json 如何解析颤振中的graphql响应

Json 如何解析颤振中的graphql响应,json,parsing,flutter,dart,graphql,Json,Parsing,Flutter,Dart,Graphql,我正在尝试在我的flatter应用程序中解析来自neo4j graphql后端的graphql响应。我正在使用flatter_graphql插件对后端进行查询。然而,当我试图解析响应(JSON)时,我得到的是“LinkHashMap不是UsersMap的子类型” 我试图修改我的序列化类,这些类将解析响应,但没有可用的 /*I HAVE EDITED THE JSON RESPONSE. THE FULL JSON RESPONSE FROM THE SERVER IS AS BELOW*/

我正在尝试在我的flatter应用程序中解析来自neo4j graphql后端的graphql响应。我正在使用flatter_graphql插件对后端进行查询。然而,当我试图解析响应(JSON)时,我得到的是“LinkHashMap不是UsersMap的子类型”

我试图修改我的序列化类,这些类将解析响应,但没有可用的


 /*I HAVE EDITED THE JSON RESPONSE. THE FULL JSON RESPONSE FROM THE SERVER IS AS BELOW*/
{
  "data": {
    "User": [
      {
        "userId": 1,
        "city": "NewYork",
        "country": "User",
        "captionType": "User",
        "state": "New York",
        "firstName": "example2",
        "lastname": "example",
        "email": "example2@gmail.com"
      }
    ]
  },
  "extensions": {
    "type": "READ_ONLY"
  }
}
下面是表示上述响应的类

@JsonSerializable()
类用户{
最终int用户标识;
最终字符串电子邮件;
最终字符串国家;
最后一个字符串名;
最终字符串性别;
最后的字符串城市;
出生日期的最终字符串;
最终字符串状态;
最终字符串标题类型;
最后一个字符串lastname;
用户({this.userId,this.email,this.country,this.firstName,this.gender,this.dateOfBirth,this.state,this.captionType,this.city,this.lastname});
factory User.fromJson(Map json)=>$UserFromJson(json);
映射到JSON()=>$UserToJson(此);
}
类用户{
最终用户名单;
用户({this.Users});
factory Users.fromJson(Map parsedJson){
var list=parsedJson['users']作为列表;
List usersList=List.map((i)=>User.fromJson(i)).toList();
返回用户(
用户:usersList
);
}
}
//下面是我单独的graphql.dart文件中的graphql配置
类GraphQLConfiguration{
静态HttpLink HttpLink=HttpLink(
uri:“http://localhost:7474/graphql/",
标题:{
HttpHeaders.authorizationHeader:*************“,
},
);
静态最终AuthLink AuthLink=AuthLink(
getToken:()async=>Bearer',
//或
//getToken:()=>“持票人”,
);
静态ValueNotifier initailizeClient(){
ValueNotifier客户端=ValueNotifier(
GraphQLClient(
缓存:InMemoryCache(),
链接:httpLink,
),
);
返回客户;
}
静态图形QLClient clientToQuery(){
返回GraphQLClient(
缓存:OptimisticCache(
dataIdFromObject:typenameDataIdFromObject),
链接:httpLink,
);
}
}
//下面是我试图解析响应的main.dart文件
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:true,
标题:“颤振图形QL客户端”,
主题:主题数据(
主样本:颜色。蓝色,
),
initialRoute:“/”,
路线:{
“/”:(上下文)=>MyHomePage(),//RootPage(auth:new auth(),),
},
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
小部件listViewWidget(列出用户){
返回MediaQuery.removePadding(
context:context,removeTop:true,
子项:ListView.builder(
itemCount:users.length,
itemBuilder:(上下文,索引){
返回容器(
子:列(
儿童:[
Text('users:$users'),
],
),
);
}),
);
}
字符串readUser=“”
质疑{
用户(用户ID:1){
用户ID
城市
国家
字幕类型
状态
名字
姓氏
电子邮件
}
}   
""";
@凌驾
小部件构建(构建上下文){
返回GraphQLProvider(
客户端:GraphQLConfiguration.initailizeClient(),
子:缓存提供程序(
孩子:脚手架(
appBar:appBar(
标题:文本(“颤振图形QL客户端”),
),
正文:查询(
选项:查询选项(
文件:readUser),
生成器:(QueryResult结果,{VoidCallback refetch,FetchMore FetchMore}){
if(result.errors!=null){
打印(“$result.errors”);
返回文本(result.errors.toString());
}if(result.errors==null){
打印(“$result”);
打印(“${result.data}”);
}如果(结果加载){
返回中心(
子项:CUPERTINOACTIVITY指示器(
半径:13.0,
));
}
返回listViewWidget(result.data);
},
)
浮动操作按钮:浮动操作按钮(
按下:(){},
子:图标(Icons.add),
),
),
));
}
}

我希望通过Users类解析信息并通过listViewWidget显示。但是我得到的是“LinkHashMap不是Users Map的子类型”。

您可以使用解析任何JSON,下面是JSON的模型类

// To parse this JSON data, do
//
//     final responseModel = responseModelFromJson(jsonString);

import 'dart:convert';

ResponseModel responseModelFromJson(String str) => ResponseModel.fromJson(json.decode(str));

String responseModelToJson(ResponseModel data) => json.encode(data.toJson());

class ResponseModel {
Data data;
Extensions extensions;

ResponseModel({
    this.data,
    this.extensions,
});

factory ResponseModel.fromJson(Map<String, dynamic> json) => ResponseModel(
    data: json["data"] == null ? null : Data.fromJson(json["data"]),
    extensions: json["extensions"] == null ? null : Extensions.fromJson(json["extensions"]),
);

Map<String, dynamic> toJson() => {
    "data": data == null ? null : data.toJson(),
    "extensions": extensions == null ? null : extensions.toJson(),
    };
}

class Data {
List<User> user;

Data({
    this.user,
});

factory Data.fromJson(Map<String, dynamic> json) => Data(
    user: json["User"] == null ? null : List<User>.from(json["User"].map((x) => User.fromJson(x))),
);

Map<String, dynamic> toJson() => {
    "User": user == null ? null : List<dynamic>.from(user.map((x) => x.toJson())),
    };
}

class User {
int userId;
String city;
String country;
String captionType;
String state;
String firstName;
String lastname;
String email;

User({
    this.userId,
    this.city,
    this.country,
    this.captionType,
    this.state,
    this.firstName,
    this.lastname,
    this.email,
});

factory User.fromJson(Map<String, dynamic> json) => User(
    userId: json["userId"] == null ? null : json["userId"],
    city: json["city"] == null ? null : json["city"],
    country: json["country"] == null ? null : json["country"],
    captionType: json["captionType"] == null ? null : json["captionType"],
    state: json["state"] == null ? null : json["state"],
    firstName: json["firstName"] == null ? null : json["firstName"],
    lastname: json["lastname"] == null ? null : json["lastname"],
    email: json["email"] == null ? null : json["email"],
);

Map<String, dynamic> toJson() => {
    "userId": userId == null ? null : userId,
    "city": city == null ? null : city,
    "country": country == null ? null : country,
    "captionType": captionType == null ? null : captionType,
    "state": state == null ? null : state,
    "firstName": firstName == null ? null : firstName,
    "lastname": lastname == null ? null : lastname,
    "email": email == null ? null : email,
    };
}

class Extensions {
String type;

Extensions({
    this.type,
});

factory Extensions.fromJson(Map<String, dynamic> json) => Extensions(
    type: json["type"] == null ? null : json["type"],
);

Map<String, dynamic> toJson() => {
    "type": type == null ? null : type,
    };
}

你的代码看起来很健壮,但我如何在listViewWidget()中实现它。这是我缺少它的关键所在。你能告诉我如何在main.dart文件中使用你的代码吗?我仍在费利特的状态管理中绞尽脑汁,但我相信你的代码可以工作,但我真的不知道如何将其称为我的listViewWidget()。很抱歉,我认为解析查询的类是错误的,因为我发布了不完整的json响应。。我在问题中编辑了graphiQL的完整响应。我没有发布完整响应。我尝试使用您最近的编辑,但仍然没有
ResponseModel  responseModel = responseModelFromJson(result.data);
return listViewWidget(responseModel.data.user);