这是我的完整代码,我不知道我犯了什么样的错误
如果你知道就改正,因为我是扑翼的初学者
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Mydata(),
);
}
}
class Mydata extends StatefulWidget {
const Mydata({super.key});
@override
State<Mydata> createState() => _MydataState();
}
class _MydataState extends State<Mydata> {
Future<List<String>> ebdetails() async {
var response =
await http.get(Uri.parse('http://117.247.181.113:8000/eb/1/'));
return jsonDecode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0,
centerTitle: true,
title: const Text(
'Json Datas',
style: TextStyle(
color: Colors.black,
),
),
backgroundColor: Colors.white,
),
body: Center(
child: FutureBuilder(
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Center(
child: Text('Data Error'),
);
} else if (snapshot.hasData) {
return Center(
child: ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, i) {
return Text(
snapshot.data![i],
);
},
));
} else {
return const CircularProgressIndicator();
}
},
future: ebdetails(),
),
),
);
}
}
- 我正在通过本地IP获取数据,但它似乎有某种错误,我已经做了,但它很难为我找到..*
- 它只显示我在Futurebuilder中给出的“数据错误”。
[
{
"id": 1,
"R_Current": -1.0,
"Y_Current": -1.0,
"B_Current": -1.0,
"R_Voltage": 208,
"Y_Voltage": 235,
"B_Voltage": 208,
"UPS_Voltage": 100,
"UPS_Current": 143.0,
"UPS_Battery": 99
}
]
3条答案
按热度按时间3zwjbxry1#
所以根据你的数据它不是字符串的列表把方法的返回类型改为
编辑:好的,您的问题是将map传递给Text()小部件,而文本小部件需要string作为参数,因此您必须将www.example.com转换snapshot.data为string,然后将其传递给Text小部件。
vecaoik12#
尝试将响应转换为自定义Dart对象
参见[1]:https://docs.flutter.dev/cookbook/networking/fetch-data
n6lpvg4x3#
我已经更改了代码并得到了答案,即snapshot.data![i].toString()