Shraddha
Shraddha

Reputation: 65

'Future<String>' can't be assigned to the parameter type 'List<Task>'

I am trying to display charts from JSON data. Here is my code,

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<Task, String>> seriesPieData;

  _generateData() {
    var pieData =
        DefaultAssetBundle.of(context).loadString("asset/jsondat.json");

    seriesPieData.add(charts.Series(
      data:pieData,
      domainFn: (Task task, _) => task.task,
      measureFn: (Task task, _) => task.taskvalue,
      id: 'Performance',
    ));
  }

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.cyan,
          title: Text('flutter charts'),
        ),
        body: TabBarView(
          children: [
            Padding(
              padding: EdgeInsets.all(8.0),
              child: Container(
                child: Center(
                  child: Column(
                    children: [
                      Text(
                        'CPU Performance',
                        style: TextStyle(
                            fontSize: 24.0, fontWeight: FontWeight.bold),
                      ),
                      SizedBox(
                        height: 10.0,
                      ),
                      Expanded(
                          child: charts.PieChart(
                        seriesPieData,
                        animate: true,
                        animationDuration: Duration(seconds: 5),
                      ))
                    ],
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class Task {
  String task;
  double taskvalue;
  Color colorval;

  // Add Constructor
  Task(this.task, this.taskvalue, this.colorval);
}

Here I have created JSON file and put it under asset file . I have no idea how to display the retrived data show to o a chart. Here is my JSON file

{
    "2016": "20",
    "2017": "70",
    "2018": "60",
    "2019": "12"
}

I have been looking for similar example but i could only find one in which data is hardcoded.

Upvotes: 0

Views: 62

Answers (1)

Andrey Gritsay
Andrey Gritsay

Reputation: 976

You need to decode loaded string from assets first, like so

final decoded = jsonDecode(DefaultAssetBundle.of(context).loadString("asset/jsondat.json")) as List;

and then you could transform it to your List<Task> like so:

final tasks = decoded.map<ListTask>((task) => Task(task['task'], task['taskvalue'], Colors.fromRBGA(task['colorval']))).toList();

According this example JSON should look like this:

[
  {
    "task": "Some task",
    "taskvalue": "some task value",
    "colorval": "#FFF000"
  },
  {
    "task": "Some task 2",
    "taskvalue": "some task value 2",
    "colorval": "#FFF00F"
  }
]

Upvotes: 1

Related Questions