I want to convert a List of type String to a list of type ChartData, cause I'm creating a graph and the data (x,y) is saved on Firestore as string, so i converted the string to a List using split and now I have to convert this List of String to List of ChartData, I'm using Sync Fusion Chart. How can I do that? I tried using
List<ChartData> b = strarray.map((e) => ChartData(5,1)).toList();
But i can't use it because When i try to use ChartData() it asks me to give the x and y.
The code: ´
import 'dart:convert';
import 'dart:ffi';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:mood3/telas/animacao.dart';
import 'package:ntp/ntp.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import '../model/GraphMood.dart';
class Graph extends StatefulWidget {
@override
State<Graph> createState() => _GraphState();
}
class _GraphState extends State<Graph> {
TooltipBehavior? _tooltipBehavior;
DateTime ntpTime = DateTime.now();
String testefinal = '';
_loadNTPTime() async {
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseFirestore db = FirebaseFirestore.instance;
setState(() async {
ntpTime = await NTP.now();
});
}
Future _recuperarNome() async {
FirebaseAuth auth2 = FirebaseAuth.instance;
User? user = auth2.currentUser;
DocumentSnapshot ds = await db.collection('usuarios')
.doc(user!.uid)
.get();
Map<String, dynamic> dss = ds.data() as Map<String, dynamic>;
setState((){
testefinal = dss["moodGraph"];
});
}
FirebaseFirestore db = FirebaseFirestore.instance;
@override
initState(){
_recuperarNome();
_tooltipBehavior = TooltipBehavior(enable: true);
super.initState();
_loadNTPTime();
}
@override
Widget build(BuildContext context) {
List<ChartData> convert(String input) {
List<ChartData> output;
try {
print(json);
output = json.decode(input);
return output;
} catch (err) {
print(err);
return [ChartData(0, 0)];
}
}
//List<ChartData> testim = testefinal.split(',');
final List<ChartData> list1 = convert(testefinal);
print(list1);
//List<String> strs = <String>[testefinal];
List<ChartData> dynamicValues = List<ChartData>.from(list1 as List);
//print(strs.runtimeType);
// List<int> numbers = strs.map(int.parse).toList();
// print(numbers.runtimeType);
// print(numbers);
print(dynamicValues);
List<String> strarray = testefinal.split(" ");
print(strarray);
List<ChartData> b = strarray.map((e) => ChartData(5,1)).toList();
// List<ChartData> stringList = (jsonDecode(testefinal) as List<ChartData>).cast<ChartData>();
//print("teste1 " + stringList.toString());
final List<ChartData> chartData = b;
return Scaffold(
body: Center(
child: Container(
child: SfCartesianChart(
primaryXAxis: CategoryAxis(),
// Chart title
title: ChartTitle(text: 'Mood Chart'),
// Enable legend
legend: Legend(isVisible: true),
// Enable tooltip
tooltipBehavior: _tooltipBehavior,
series: <ChartSeries>[
// Renders line chart
LineSeries<ChartData, int>(
dataSource: chartData,
xValueMapper: (ChartData data, _) => data.x.toInt(),
yValueMapper: (ChartData data, _) => data.y
)
]
)
)
)
);
}
}
class ChartData {
ChartData(this.x, this.y);
final double x;
final double y;
}
Basically, if i try to use the value " e " of the map on ChartData(x,y)
List<ChartData> b = strarray.map((e) => ChartData(5,1) <---- here ).toList(); it says Error: Too few positional arguments: 2 required, 1 given. So i can't use the map here. And if i try to add the List of Strings to the list List chartData = [] it says Error: A value of type 'List String' can't be assigned to a variable of type 'List ChartData'. So i'm thinking on add the List of strings to the List of ChartData, or convert the list of strings to list of chartData, but i don't know how to do that