Flutter - Iterate chart with json











up vote
0
down vote

favorite












I'm trying to show a chart with a api json data. I made some a iteration and save result on a global variable, then i put this variable in chart, but variable return null, does not have any data. If i print list in foreach has data but if I print in widget is null. I read this but code looks very different so i can´t follow.



Variables



  List<charts.Series<TimeSeriesSales, DateTime>> seriesList;
List<TimeSeriesSales> data = ;


Get data



final String url = 'URL';

class TimeSeriesSales {
final DateTime fecha;
final double valor;

TimeSeriesSales(this.fecha, this.valor);
}


Future<Null> getUserDetails() async {
final response = await http.get(
url,
headers: {
HttpHeaders.authorizationHeader:
"HEADER "
},
);
final responseJson = json.decode(response.body);
setState(() {
for (Map user in responseJson) {
var fecha= new DateTime.fromMillisecondsSinceEpoch(user['fecha']).toLocal();
data.add(TimeSeriesSales(new DateTime(fecha.year, fecha.month,fecha.day), user['valor']));
print (data.toString());
}
});
}


Init State



void initState() {
super.initState();
getUserDetails();
seriesList = _createSampleData();
animate = false;
}


Fill TimeSeries



 List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
return [
new charts.Series<TimeSeriesSales, DateTime>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (TimeSeriesSales valor, _) => valor.fecha,
measureFn: (TimeSeriesSales valor, _) => valor.valor,
data: data,
)
];
}
}


Widget



 Widget build(BuildContext context) {
return new Scaffold(
new Expanded(
child: new charts.TimeSeriesChart(
seriesList,
animate: animate,
defaultRenderer: new charts.BarRendererConfig<DateTime>(),
domainAxis: new charts.DateTimeAxisSpec(usingBarRenderer: true),
defaultInteractions: false,
behaviors: [
new charts.SelectNearest(),
new charts.DomainHighlighter()
],
),
),

)


Anybody knows why list does not have data in widget? And somebody know what is the right way to iterate chart on init?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to show a chart with a api json data. I made some a iteration and save result on a global variable, then i put this variable in chart, but variable return null, does not have any data. If i print list in foreach has data but if I print in widget is null. I read this but code looks very different so i can´t follow.



    Variables



      List<charts.Series<TimeSeriesSales, DateTime>> seriesList;
    List<TimeSeriesSales> data = ;


    Get data



    final String url = 'URL';

    class TimeSeriesSales {
    final DateTime fecha;
    final double valor;

    TimeSeriesSales(this.fecha, this.valor);
    }


    Future<Null> getUserDetails() async {
    final response = await http.get(
    url,
    headers: {
    HttpHeaders.authorizationHeader:
    "HEADER "
    },
    );
    final responseJson = json.decode(response.body);
    setState(() {
    for (Map user in responseJson) {
    var fecha= new DateTime.fromMillisecondsSinceEpoch(user['fecha']).toLocal();
    data.add(TimeSeriesSales(new DateTime(fecha.year, fecha.month,fecha.day), user['valor']));
    print (data.toString());
    }
    });
    }


    Init State



    void initState() {
    super.initState();
    getUserDetails();
    seriesList = _createSampleData();
    animate = false;
    }


    Fill TimeSeries



     List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
    return [
    new charts.Series<TimeSeriesSales, DateTime>(
    id: 'Sales',
    colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
    domainFn: (TimeSeriesSales valor, _) => valor.fecha,
    measureFn: (TimeSeriesSales valor, _) => valor.valor,
    data: data,
    )
    ];
    }
    }


    Widget



     Widget build(BuildContext context) {
    return new Scaffold(
    new Expanded(
    child: new charts.TimeSeriesChart(
    seriesList,
    animate: animate,
    defaultRenderer: new charts.BarRendererConfig<DateTime>(),
    domainAxis: new charts.DateTimeAxisSpec(usingBarRenderer: true),
    defaultInteractions: false,
    behaviors: [
    new charts.SelectNearest(),
    new charts.DomainHighlighter()
    ],
    ),
    ),

    )


    Anybody knows why list does not have data in widget? And somebody know what is the right way to iterate chart on init?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to show a chart with a api json data. I made some a iteration and save result on a global variable, then i put this variable in chart, but variable return null, does not have any data. If i print list in foreach has data but if I print in widget is null. I read this but code looks very different so i can´t follow.



      Variables



        List<charts.Series<TimeSeriesSales, DateTime>> seriesList;
      List<TimeSeriesSales> data = ;


      Get data



      final String url = 'URL';

      class TimeSeriesSales {
      final DateTime fecha;
      final double valor;

      TimeSeriesSales(this.fecha, this.valor);
      }


      Future<Null> getUserDetails() async {
      final response = await http.get(
      url,
      headers: {
      HttpHeaders.authorizationHeader:
      "HEADER "
      },
      );
      final responseJson = json.decode(response.body);
      setState(() {
      for (Map user in responseJson) {
      var fecha= new DateTime.fromMillisecondsSinceEpoch(user['fecha']).toLocal();
      data.add(TimeSeriesSales(new DateTime(fecha.year, fecha.month,fecha.day), user['valor']));
      print (data.toString());
      }
      });
      }


      Init State



      void initState() {
      super.initState();
      getUserDetails();
      seriesList = _createSampleData();
      animate = false;
      }


      Fill TimeSeries



       List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
      return [
      new charts.Series<TimeSeriesSales, DateTime>(
      id: 'Sales',
      colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
      domainFn: (TimeSeriesSales valor, _) => valor.fecha,
      measureFn: (TimeSeriesSales valor, _) => valor.valor,
      data: data,
      )
      ];
      }
      }


      Widget



       Widget build(BuildContext context) {
      return new Scaffold(
      new Expanded(
      child: new charts.TimeSeriesChart(
      seriesList,
      animate: animate,
      defaultRenderer: new charts.BarRendererConfig<DateTime>(),
      domainAxis: new charts.DateTimeAxisSpec(usingBarRenderer: true),
      defaultInteractions: false,
      behaviors: [
      new charts.SelectNearest(),
      new charts.DomainHighlighter()
      ],
      ),
      ),

      )


      Anybody knows why list does not have data in widget? And somebody know what is the right way to iterate chart on init?










      share|improve this question













      I'm trying to show a chart with a api json data. I made some a iteration and save result on a global variable, then i put this variable in chart, but variable return null, does not have any data. If i print list in foreach has data but if I print in widget is null. I read this but code looks very different so i can´t follow.



      Variables



        List<charts.Series<TimeSeriesSales, DateTime>> seriesList;
      List<TimeSeriesSales> data = ;


      Get data



      final String url = 'URL';

      class TimeSeriesSales {
      final DateTime fecha;
      final double valor;

      TimeSeriesSales(this.fecha, this.valor);
      }


      Future<Null> getUserDetails() async {
      final response = await http.get(
      url,
      headers: {
      HttpHeaders.authorizationHeader:
      "HEADER "
      },
      );
      final responseJson = json.decode(response.body);
      setState(() {
      for (Map user in responseJson) {
      var fecha= new DateTime.fromMillisecondsSinceEpoch(user['fecha']).toLocal();
      data.add(TimeSeriesSales(new DateTime(fecha.year, fecha.month,fecha.day), user['valor']));
      print (data.toString());
      }
      });
      }


      Init State



      void initState() {
      super.initState();
      getUserDetails();
      seriesList = _createSampleData();
      animate = false;
      }


      Fill TimeSeries



       List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
      return [
      new charts.Series<TimeSeriesSales, DateTime>(
      id: 'Sales',
      colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
      domainFn: (TimeSeriesSales valor, _) => valor.fecha,
      measureFn: (TimeSeriesSales valor, _) => valor.valor,
      data: data,
      )
      ];
      }
      }


      Widget



       Widget build(BuildContext context) {
      return new Scaffold(
      new Expanded(
      child: new charts.TimeSeriesChart(
      seriesList,
      animate: animate,
      defaultRenderer: new charts.BarRendererConfig<DateTime>(),
      domainAxis: new charts.DateTimeAxisSpec(usingBarRenderer: true),
      defaultInteractions: false,
      behaviors: [
      new charts.SelectNearest(),
      new charts.DomainHighlighter()
      ],
      ),
      ),

      )


      Anybody knows why list does not have data in widget? And somebody know what is the right way to iterate chart on init?







      charts dart flutter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      El Hombre Sin Nombre

      153114




      153114
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You aren't re-creating the series after you've fetched and parsed the data. Call seriesList = _createSampleData(); again after iterating the responseData, inside the setState.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373149%2fflutter-iterate-chart-with-json%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You aren't re-creating the series after you've fetched and parsed the data. Call seriesList = _createSampleData(); again after iterating the responseData, inside the setState.






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              You aren't re-creating the series after you've fetched and parsed the data. Call seriesList = _createSampleData(); again after iterating the responseData, inside the setState.






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                You aren't re-creating the series after you've fetched and parsed the data. Call seriesList = _createSampleData(); again after iterating the responseData, inside the setState.






                share|improve this answer












                You aren't re-creating the series after you've fetched and parsed the data. Call seriesList = _createSampleData(); again after iterating the responseData, inside the setState.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                Richard Heap

                4,6142212




                4,6142212






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373149%2fflutter-iterate-chart-with-json%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules