我正在尝试调用 api,以使用 syncfusion_flutter_charts 上的数据,但是,我收到一个错误:参数类型“字符串函数(SensorData,int,动态)”无法分配给参数类型“字符串函数( SensorData, int)'.dart(argument_type_not_assignable)。
我究竟做错了什么?
我的代码:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:syncfusion_flutter_charts/sparkcharts.dart';
import 'package:http/http.dart' as http;
// import 'package:http/http.dart' as http;
class LineChartHum extends StatefulWidget {
// LineChartHum({Key? key}) : super(key: key);
@override
_LineChartHumState createState() => _LineChartHumState();
}
class _LineChartHumState extends State<LineChartHum> {
@override
void initState() {
loadSensorData();
super.initState();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.all(16),
height: 300,
child: SfCartesianChart(
zoomPanBehavior: ZoomPanBehavior(
//
enablePinching: true,
enableDoubleTapZooming: true,
enablePanning: true,
enableSelectionZooming: true,
enableMouseWheelZooming: true,
),
primaryXAxis: CategoryAxis(),
title: ChartTitle(text: 'Humidity Sensor'),
tooltipBehavior: TooltipBehavior(enable: true),
series: <LineSeries<SensorData, String>>[
LineSeries<SensorData, String>(
dataSource: chartData,
xValueMapper: (SensorData, sensors, _) => sensors.timeStamp,
yValueMapper: (SensorData, sensors, _) => sensors.sensorValue,
)
]
))
],
),
);
}
List<SensorData> chartData = [];
Future loadSensorData() async {
final jsonResponse = json.decode(toString());
// print(jsonString);
// print(jsonResponse);
setState(() {
for (Map i in jsonResponse) chartData.add(SensorData.fromJson(i));
});
}
}
Future<String> fetchUsers() async {
var url = Uri.parse('https://api.thingspeak.com/channels/960293/fields/1.json');
http.Response response = await http.get(url);
print(response.body);
return response.body.toString();
}
class SensorData {
SensorData({
this.timeStamp,
this.sensorValue,
});
DateTime timeStamp;
int sensorValue;
factory SensorData.fromJson(Map<String, dynamic> json) {
return SensorData(
timeStamp: json['timeStamp'],
sensorValue: json['SensorValue'],
);
}
}
我的 API 喜欢: https ://api.thingspeak.com/channels/960293/fields/1.json 图表的外观: https ://thingspeak.com/channels/960293 。我希望它至少显示在此链接:https ://thingspeak.com/channels/960293