1

我正在尝试调用 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

4

1 回答 1

0

我们在最后分析了您的场景并检查了您的代码片段,我们发现您在设置系列的 xValueMapper 和 yValueMapper 属性的值时出现了语法错误。请参考下面的代码片段以供参考。

LineSeries<SensorData, String>(
                    dataSource: chartData,
                    /// Syntax error due to specifying comma (,) between the class and its object_name
                    /// xValueMapper: (SensorData, sensors, _) => sensors.timeStamp,  
                    /// yValueMapper: (SensorData, sensors, _) => sensors.sensorValue,
                   

                    /// Correct Syntax
                    xValueMapper: (SensorData sensors, _) => sensors.timeStamp,  
                    yValueMapper: (SensorData sensors, _) => sensors.sensorValue,
                  )

知识库:https ://www.syncfusion.com/kb/11674

于 2021-09-21T16:19:44.147 回答