我正在尝试通过Flutter SDK使用HTTP GET在线获取数据。我正在尝试使用此代码https://github.com/RaglandCodes/Flutter-basic-API/blob/master/lib/main.dart,但它显示的数据类型为
的错误不能将参数类型'String'分配给参数类型'int'
这是错误部分
new Card(
child: new Container(
child: new Text(data[index]['name']), //error red underlying with 'name'
padding: EdgeInsets.all(20),
),
这是我的main.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
void main() {
runApp(new MaterialApp(home: new HomePage(),));
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String url="https://swapi.co/api/people/";
List<String> data;
/*onCreate*/
@override
void initState() {
// TODO: implement initState
super.initState();
getJSONData(); //method
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("my JSON app")
),
body: new ListView.builder(
// itemCount: 1,
//itemCount: data==null ? 0 :data.length ,
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index){
return new Container(
child: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Card(
child: new Container(
child: new Text(data[index]['name']),
padding: EdgeInsets.all(20),
),
)
],
),
),
);
},
),
);
}
/*method*/ //RT is Future<String>
Future<String> getJSONData() async{
var response =await http.get(
Uri.encodeFull(url),
headers: {"Accept": "application/json"}
);
print(response.body);
debugPrint(response.body);
setState(() {
var convertDataToJson= json.decode(response.body);
data=convertDataToJson['results'];
});
return "Success";
}
}
答案 0 :(得分:42)
这对我有用
http.get(Uri.https('https://swapi.co', 'api/people'));
或
http.get(Uri.parse('https://swapi.co/api/people'));
答案 1 :(得分:12)
原因是 http.get 不能再处理字符串了。您需要使用 Uri 来解析 HTTP 地址。
我遇到了同样的问题,我用这种方法修复了它:
var url = Uri.parse("https://your_api_link");
然后像这样调用它(附加http):
http.Response response = await http.get(url);
这应该有效。
答案 2 :(得分:3)
var response = await http.get(Uri.parse(url));
答案 3 :(得分:1)
这对我有用
void getData() async {
Response response = await
http.get(Uri.parse("https://jsonplaceholder.typicode.com/todos/1"));
print(response.body);
}
答案 4 :(得分:0)
您必须将data
变量设置为List
类型。
应该可以:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
void main() {
runApp(new MaterialApp(
home: new HomePage(),
));
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String url = "https://swapi.co/api/people/";
List data;
/*onCreate*/
@override
void initState() {
// TODO: implement initState
super.initState();
getJSONData(); //method
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text("my JSON app")),
body: new ListView.builder(
// itemCount: 1,
//itemCount: data==null ? 0 :data.length ,
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
return new Container(
child: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Card(
child: new Container(
child: new Text(data[index]['name'] ?? ''),
padding: EdgeInsets.all(20),
),
)
],
),
),
);
},
),
);
}
/*method*/ //RT is Future<String>
Future<String> getJSONData() async {
var response = await http
.get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
print(response.body);
debugPrint(response.body);
setState(() {
var convertDataToJson = json.decode(response.body);
data = convertDataToJson['results'];
});
return "Success";
}
}
答案 5 :(得分:0)
你可以这样使用
Future<http.Response> fetchAlbum() {return http.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));}
答案 6 :(得分:0)
将 http.get(url) 改为 http.get(Uri.parse(url));
答案 7 :(得分:0)
这对我有用
http.Response respobse= await
http.get(Uri.parse("https://jsonplaceholder.typicode.com/photos"));
Uri.parse(json Url);
使用这个。
答案 8 :(得分:0)
这个可以用
String url = '$api?lat=$latitude&lon=$longitude&APPID=$appId';
http.Response response = await http.get(Uri.parse(url));
答案 9 :(得分:-1)
记录器应该说出错误所在的行,因此我不会下载您的代码来尝试并找到它。
您需要做的就是将String值转换为整数。找到应为整数的值时,请使用:
int.parse(VALUE)
这里是参考:https://code-maven.com/slides/dart-programming/casting-string-to-int