错误:类型“

时间:2020-05-14 16:51:25

标签: flutter

我正在尝试显示来自Json数据的图表。从本地文件获取的Json数据。我不了解此错误,也未找到与此相关的任何解决方案。我应该如何解决这个错误 这是我的代码,

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;


class HomePage extends StatefulWidget {
  final Widget child;

  HomePage({Key key, this.child}) : super(key: key);

  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<charts.Series<Sales, String>> seriesBarData;

  _generateData() async {
    final decoded = await DefaultAssetBundle.of(context).loadString("asset/data.json") as List;

    seriesBarData.add(charts.Series(
      data:decoded,
      domainFn: (Sales sales, _) => sales.saleyear,
      measureFn: (Sales sales, _) => int.parse(sales.saleval),
      id: 'Performance',
    ));
  }

  @override
  void initState() {
    super.initState();
    seriesBarData = List<charts.Series<Sales, String>>();
    _generateData();
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.cyan,
          title: Text('flutter charts'),
        ),
        body:  Column(
                    children: [
                      Text(
                        'Sales By Year',
                        style: TextStyle(
                            fontSize: 24.0, fontWeight: FontWeight.bold),
                      ),
                      SizedBox(
                        height: 10.0,
                      ),
                      Expanded(
                          child: charts.BarChart(
                            seriesBarData,
                            animate: true,
                            animationDuration: Duration(seconds: 5),
                          ),
                      ),
                    ],
                  ),
                );

  }
}

这是模型类,

class Sales {
      String saleyear;
      String saleval;


      // Add Constructor
      Sales(this.saleyear, this.saleval);
    }

我正在尝试从本地文件获取JSON数据。这是JSON文件,

[

  {
    "saleyear": "2015",
    "saleval": "10"
  },
  {
    "saleyear": "2016",
    "saleval": "30"
  },
  {
    "saleyear": "2017",
    "saleval": "50"
  },
  {
    "saleyear": "2018",
    "saleval": "10"
  }
]

3 个答案:

答案 0 :(得分:1)

您必须先解码json,然后再创建销售对象。

我对您的代码进行了很少的更改,希望对您有所帮助。

class HomePage extends StatefulWidget {
  final Widget child;

  HomePage({Key key, this.child}) : super(key: key);

  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<charts.Series<Sales, String>> seriesBarData;

  _generateData() async {
    final load =
        await DefaultAssetBundle.of(context).loadString("assets/delete.json");
    var decoded = json.decode(load);
    List<Sales> sales = [];
    for (var item in decoded) {
      sales.add(Sales.fromJson(item));
    }

    seriesBarData.add(charts.Series(
      data: sales,
      domainFn: (Sales sales, _) => sales.saleyear,
      measureFn: (Sales sales, _) => int.parse(sales.saleval),
      id: 'Performance',
    ));
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    seriesBarData = List<charts.Series<Sales, String>>();
    _generateData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.cyan,
        title: Text('flutter charts'),
      ),
      body: Column(
        children: [
          Text(
            'Sales By Year',
            style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
          ),
          SizedBox(
            height: 10.0,
          ),
          seriesBarData.length > 0
              ? Expanded(
                  child: charts.BarChart(
                    seriesBarData,
                    animate: true,
                    animationDuration: Duration(seconds: 5),
                  ),
                )
              : Container(),
        ],
      ),
    );
  }
}

class Sales {
  String saleyear;
  String saleval;

  Sales(this.saleyear, this.saleval);

  Sales.fromJson(Map<String, dynamic> json) {
    saleyear = json['saleyear'];
    saleval = json['saleval'];
  }
}

答案 1 :(得分:0)

您可以尝试更改吗?

final decoded = await DefaultAssetBundle.of(context).loadString("asset/data.json") as List<Sales>;

或如果它不起作用,

Future<Sales> _generateData() async {...

答案 2 :(得分:0)

您导入了模型吗?我看不到您的代码。