Realtime Data without Push ID

Hi everyone,

I want to access the real time data and display it on the chart, but its not showing and i think the problem is with the push id.

My code is below and the structure of my DB is attached, please help me out!






2 Replies

SA Sarah March 2, 2022 05:02 PM UTC

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:http/http.dart' as http;



class MyHomePage extends StatefulWidget {
// ignore: prefer_const_constructors_in_immutables
MyHomePage({Key? key}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
List<SalesData> chartData = [];

@override
void initState() {
loadSalesData();
super.initState();
}

Future loadSalesData() async {
final String jsonString = await getJsonFromFirebase();
final dynamic jsonResponse = json.decode(jsonString);
for (Map<String, dynamic> i in jsonResponse)
chartData.add(SalesData.fromJson(i));
}

Future<String> getJsonFromFirebase() async {
String url = "";
http.Response response = await http.get(Uri.parse(url));
return response.body;
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter chart'),
),
body: Center(
child: FutureBuilder(
future: getJsonFromFirebase(),
builder: (context, snapShot) {
if (snapShot.hasData) {
return SfCartesianChart(
primaryXAxis: CategoryAxis(),
// Chart title
title: ChartTitle(text: 'HeartRate Analysis'),
series: <ChartSeries<SalesData, String>>[
LineSeries<SalesData, String>(
dataSource: chartData,
xValueMapper: (SalesData sales, _) => sales.month,
yValueMapper: (SalesData sales, _) => sales.sales,
// Enable data label
dataLabelSettings:
DataLabelSettings(isVisible: true))
]);
} else {
return Card(
elevation: 5.0,
child: Container(
height: 100,
width: 400,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text('Retriving Firebase data...',
style: TextStyle(fontSize: 20.0)),
Container(
height: 40,
width: 40,
child: CircularProgressIndicator(
semanticsLabel: 'Retriving Firebase data',
valueColor: AlwaysStoppedAnimation<Color>(
Colors.blueAccent),
backgroundColor: Colors.grey[300],
),
),
],
),
),
),
);
}
}),
));
}
}

class SalesData {
SalesData(this.month, this.sales);

final String month;
final int sales;

factory SalesData.fromJson(Map<String, dynamic> parsedJson) {
return SalesData(
parsedJson['Time'].toString(),
parsedJson['HeartRate'],
);
}
}


YG Yuvaraj Gajaraj Syncfusion Team March 3, 2022 03:21 PM UTC

Hi Sarah, 

We have created the sample in which we have created the JSON data locally as per the structure you have in your database and rendered a line chart. You can modify this as per your requirement, we have attached the sample and KB below for your reference. 


  
Regards, 
Yuvaraj. 


Loader.
Up arrow icon