gpt4 book ai didi

flutter - Flutter,FutureBuilder快照错误: 'int'类型不是 'String'类型的子类型

转载 作者:行者123 更新时间:2023-12-03 04:00:49 28 4
gpt4 key购买 nike

当我尝试从Post获取数据时,我因此错误而失败。

enter image description here

似乎nodeid变量被视为int类型。
所以我在下面尝试了

 factory Post.fromJson(Map<String, dynamic> json) {
return Post(
nodeid: nodeid.toString(),

但这是行不通的。

这是下面的完整代码。
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<Post> fetchPost() async {
var params = { // Request를 위한 parameters 만듦.
//TODO 서비스키 UTF-8 encoding이 필요함.
"ServiceKey": utf8.encode('t8iGBTBFQc5nMq9ytuMZQ7jxR8ZMzohG%2F5o0i80f%2BehjFhMpkADfArAvFk%2BjHsPMFFEThQgejzujdpfou%2FDWng%3D%3D'),
"cityCode": Uri.encodeFull("25"),
"nodeId": Uri.encodeFull("DJB8001793ND"),
};

Uri uri = Uri.parse('http://openapi.tago.go.kr/openapi/service/ArvlInfoInqireService/getSttnAcctoArvlPrearngeInfoList'); // URI 지정.
final newURI = uri.replace(queryParameters: params); // queryParameters를 넣어 URI를 다시 만듦.

final response = await http.get(
newURI,
headers: {
"Accept": "application/json", // The server is returning you XML, but you are trying to parse that as if it is JSON.
},
);

/*
final responseJson = jsonDecode(response.body);
debugPrint('$responseJson'); //
print('$responseJson'); //{response: {header: {resultCode: 99, resultMsg: SERVICE KEY IS NOT REGISTERED ERROR.}}}
print(Uri.encodeFull("DJB8001793ND")); //I/flutter ( 7299): DJB8001793ND
*/
//return Post.fromJson(responseJson);

if (response.statusCode == 200) {
// If server returns an OK response, parse the JSON.
return Post.fromJson(jsonDecode(response.body));
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post');
}
}

class Post {
final String nodeid;
//final String nodenm;
//final int arrtime;
//final int routeno;

Post({
this.nodeid,
//this.nodenm,
//this.arrtime,
//this.routeno
});

factory Post.fromJson(Map<String, dynamic> json) {
return Post(
nodeid: json['nodeid'],
//nodenm: json['nodenm'],
//arrtime: json['arrtime'],
//routeno: json['routeno'],
);
}
}

void main() => runApp(MyApp(post: fetchPost()));

class MyApp extends StatelessWidget {
final Future<Post> post;
MyApp({Key key, this.post}) : super(key: key);


@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Fetch Data Example'),
),
body: Center(
child: FutureBuilder<Post>(
future: post,
builder: (context, snapshot) {
if (snapshot.hasData) { // 만약에 Post에서 데이터가 존재한다면
return Text(snapshot.data.nodeid); // 데이터 스냅샷을 보여준다.
} else if (snapshot.hasError) { // << I got an error from here.
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
),
),
);
}
}

这是XML文件。

enter image description here

我有什么想念的吗?

最佳答案

如果服务器返回XML,则设置标记不会更改数据类型。

其次,我认为api无法正常工作。

<?xml version="1.0" encoding="UTF-8"?>
<response>
<header>
<resultCode>99</resultCode>
<resultMsg>SERVICE KEY IS NOT REGISTERED ERROR.</resultMsg>
</header>
</response>

我尝试查看 https://tago.go.kr/来自己找到该API,以获取更多信息。但这是韩文。

原来的问题。

如果api返回的数据是XML格式的任何数据。尽管您可以将其转换为JSON没什么大不了的,但它还是照常营业。
import 'package:xml2json/xml2json.dart'; // add this line
...
Xml2Json xml2json = new Xml2Json(); //Make an instance.

// Response is in xml, parse using xml2json
xml2json.parse(response.body);

var jsondata = xml2json.toGData();
// now we have data in json format

// and are ok to continue
var data = json.decode(jsondata);
// data is json do what you need to
...

关于flutter - Flutter,FutureBuilder快照错误: 'int'类型不是 'String'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56979553/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com