Created
June 26, 2022 11:20
-
-
Save isanecat/9194afac7dad42bc6599a17c7e5960fd to your computer and use it in GitHub Desktop.
Riverpod returns null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:fl_chart/fl_chart.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
import 'package:tranzek/Constants/rotalar.dart'; | |
import 'package:tranzek/Models/Import/tranzeks.dart'; | |
import 'package:tranzek/RiverpodStates/admin_provider.dart'; | |
import 'package:tranzek/RiverpodStates/i_rem_report_provider.dart'; | |
class I_Rem_Report extends ConsumerStatefulWidget { | |
final String companyID; | |
final String companyName; | |
const I_Rem_Report({ | |
Key? key, | |
required this.companyID, | |
required this.companyName, | |
}) : super(key: key); | |
@override | |
_I_Rem_ReportState createState() => _I_Rem_ReportState(); | |
} | |
class _I_Rem_ReportState extends ConsumerState<I_Rem_Report> { | |
final db = FirebaseFirestore.instance; | |
User? currentUser = FirebaseAuth.instance.currentUser; | |
final Color barBackgroundColor = const Color(0xff72d8bf); | |
final Duration animDuration = const Duration(milliseconds: 250); | |
int touchedIndex = -1; | |
int importIdCounter = 0; | |
@override | |
Widget build(BuildContext context) { | |
//dogrudan url yazarak ulaşmaya çalıştığında | |
//login ise devam et, değilse anasayfaya git. | |
if (currentUser == null) { | |
Navigator.pushNamed(context, Rotalar.webHomePage); | |
} | |
final double ekranEni = MediaQuery.of(context).size.width; | |
final double ekranBoyu = MediaQuery.of(context).size.height; | |
String? adminID = ref.watch(adminStateProvider).value; | |
AsyncValue juneTotalValue = ref.watch(iRemReportsFamily(I_Rem_Params( | |
adminID: adminID!, companyID: widget.companyID, month: 5))); | |
print("widget taki değer : " + juneTotalValue.value.toString()); | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("Peşin Ödeme Raporu"), | |
), | |
body: Center( | |
child: AspectRatio( | |
aspectRatio: ekranEni / ekranBoyu, | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Card( | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.circular(18)), | |
color: const Color(0xff81e5cd), | |
child: Stack( | |
children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.all(16), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
mainAxisAlignment: MainAxisAlignment.start, | |
mainAxisSize: MainAxisSize.max, | |
children: [ | |
juneTotalValue.when( | |
data: (data) => Text(data.value.toString()), | |
error: (error, stack) => Text(error.toString()), | |
loading: () => const CircularProgressIndicator()), | |
const SizedBox( | |
height: 38, | |
), | |
Expanded( | |
child: Padding( | |
padding: | |
const EdgeInsets.symmetric(horizontal: 8.0), | |
child: BarChart( | |
mainBarData(), | |
swapAnimationDuration: animDuration, | |
), | |
), | |
), | |
const SizedBox( | |
height: 12, | |
), | |
], | |
), | |
), | |
], | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
BarChartGroupData makeGroupData( | |
int x, | |
double y, { | |
bool isTouched = false, | |
Color barColor = Colors.white, | |
double width = 22, | |
List<int> showTooltips = const [], | |
}) { | |
return BarChartGroupData( | |
x: x, | |
barRods: [ | |
BarChartRodData( | |
toY: isTouched ? y + 1 : y, | |
color: isTouched ? Colors.yellow : barColor, | |
width: width, | |
borderSide: isTouched | |
? BorderSide(color: Colors.yellow.shade900, width: 1) | |
: const BorderSide(color: Colors.white70, width: 0), | |
backDrawRodData: BackgroundBarChartRodData( | |
show: true, | |
toY: 20, | |
color: barBackgroundColor, | |
), | |
), | |
], | |
showingTooltipIndicators: showTooltips, | |
); | |
} | |
List<BarChartGroupData> showingGroups() => List.generate(12, (i) { | |
switch (i) { | |
case 0: | |
// X parametresi o ayın title'ını Y parametresi o ayın değerini gösterir. | |
return makeGroupData(0, 15000, isTouched: i == touchedIndex); | |
case 1: | |
return makeGroupData(1, 6500, isTouched: i == touchedIndex); | |
case 2: | |
return makeGroupData(2, 10000, isTouched: i == touchedIndex); | |
case 3: | |
return makeGroupData(3, 7.5, isTouched: i == touchedIndex); | |
case 4: | |
return makeGroupData(4, 9000, isTouched: i == touchedIndex); | |
case 5: | |
return makeGroupData(5, 55555, isTouched: i == touchedIndex); | |
case 6: | |
return makeGroupData(6, 6500, isTouched: i == touchedIndex); | |
case 7: | |
return makeGroupData(7, 77000, isTouched: i == touchedIndex); | |
case 8: | |
return makeGroupData(8, 45300, isTouched: i == touchedIndex); | |
case 9: | |
return makeGroupData(9, 15000, isTouched: i == touchedIndex); | |
case 10: | |
return makeGroupData(10, 0, isTouched: i == touchedIndex); | |
case 11: | |
return makeGroupData(11, 0, isTouched: i == touchedIndex); | |
default: | |
return throw Error(); | |
} | |
}); | |
BarChartData mainBarData() { | |
return BarChartData( | |
barTouchData: BarTouchData( | |
touchTooltipData: BarTouchTooltipData( | |
tooltipBgColor: Colors.blueGrey, | |
getTooltipItem: (group, groupIndex, rod, rodIndex) { | |
String month; | |
// Çubuklardaki tooltipler | |
switch (group.x.toInt()) { | |
case 0: | |
month = 'Ocak'; | |
break; | |
case 1: | |
month = 'Şubat'; | |
break; | |
case 2: | |
month = 'Mart'; | |
break; | |
case 3: | |
month = 'Nisan'; | |
break; | |
case 4: | |
month = 'Mayıs'; | |
break; | |
case 5: | |
month = 'Haziran'; | |
break; | |
case 6: | |
month = 'Temmuz'; | |
break; | |
case 7: | |
month = 'Ağustos'; | |
break; | |
case 8: | |
month = 'Eylül'; | |
break; | |
case 9: | |
month = 'Ekim'; | |
break; | |
case 10: | |
month = 'Kasım'; | |
break; | |
case 11: | |
month = 'Aralık'; | |
break; | |
default: | |
throw Error(); | |
} | |
return BarTooltipItem( | |
month + '\n', | |
const TextStyle( | |
color: Colors.white, | |
fontWeight: FontWeight.bold, | |
fontSize: 18, | |
), | |
children: <TextSpan>[ | |
TextSpan( | |
text: (rod.toY - 1).toString(), | |
style: const TextStyle( | |
color: Colors.yellow, | |
fontSize: 16, | |
fontWeight: FontWeight.w500, | |
), | |
), | |
], | |
); | |
}), | |
touchCallback: (FlTouchEvent event, barTouchResponse) { | |
setState(() { | |
if (!event.isInterestedForInteractions || | |
barTouchResponse == null || | |
barTouchResponse.spot == null) { | |
touchedIndex = -1; | |
return; | |
} | |
touchedIndex = barTouchResponse.spot!.touchedBarGroupIndex; | |
}); | |
}, | |
), | |
titlesData: FlTitlesData( | |
show: true, | |
rightTitles: AxisTitles( | |
sideTitles: SideTitles(showTitles: false), | |
), | |
topTitles: AxisTitles( | |
sideTitles: SideTitles(showTitles: false), | |
), | |
bottomTitles: AxisTitles( | |
sideTitles: SideTitles( | |
showTitles: true, | |
getTitlesWidget: getTitles, | |
reservedSize: 38, | |
), | |
), | |
leftTitles: AxisTitles( | |
sideTitles: SideTitles( | |
showTitles: false, | |
), | |
), | |
), | |
borderData: FlBorderData( | |
show: false, | |
), | |
barGroups: showingGroups(), | |
gridData: FlGridData(show: false), | |
); | |
} | |
// Alt Çubukta Ay isimlerinin yazıldığı widget | |
Widget getTitles(double value, TitleMeta meta) { | |
const style = TextStyle( | |
color: Colors.deepPurple, | |
fontWeight: FontWeight.bold, | |
fontSize: 16, | |
); | |
Widget text; | |
switch (value.toInt()) { | |
case 0: | |
text = const Text('Ocak', style: style); | |
break; | |
case 1: | |
text = const Text('Şubat', style: style); | |
break; | |
case 2: | |
text = const Text('Mart', style: style); | |
break; | |
case 3: | |
text = const Text('Nisan', style: style); | |
break; | |
case 4: | |
text = const Text('Mayıs', style: style); | |
break; | |
case 5: | |
text = const Text('Haziran', style: style); | |
break; | |
case 6: | |
text = const Text('Temmuz', style: style); | |
break; | |
case 7: | |
text = const Text('Ağustos', style: style); | |
break; | |
case 8: | |
text = const Text('Eylül', style: style); | |
break; | |
case 9: | |
text = const Text('Ekim', style: style); | |
break; | |
case 10: | |
text = const Text('Kasım', style: style); | |
break; | |
case 11: | |
text = const Text('Aralık', style: style); | |
break; | |
default: | |
text = const Text('', style: style); | |
break; | |
} | |
return SideTitleWidget( | |
axisSide: meta.axisSide, | |
space: 16, | |
child: text, | |
); | |
} | |
Future<double> getMonthValue(int month) async { | |
String? adminID = ref.read(adminStateProvider).value; | |
double monthlyTotal = 0; | |
await db | |
.collection("Admins") | |
.doc(adminID) | |
.collection("Companies") | |
.doc(widget.companyID) | |
.collection("Imports") | |
.get() | |
.then((dSnap1) { | |
dSnap1.docs.forEach((element1) { | |
db | |
.collection("Admins") | |
.doc(adminID) | |
.collection("Companies") | |
.doc(widget.companyID) | |
.collection("Imports") | |
.doc(element1["Import_ID"]) | |
.collection("Tranzeks") | |
.get() | |
.then( | |
(dSnap2) { | |
dSnap2.docs.forEach((element2) { | |
Tranzeks tranzek = Tranzeks.docToObject(element2); | |
if (month == tranzek.creationTime.toDate().month) { | |
monthlyTotal = monthlyTotal + tranzek.amount; | |
} | |
}); | |
}, | |
); | |
}); | |
}); | |
return monthlyTotal; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
class I_Rem_Params { | |
final String adminID; | |
final String companyID; | |
final int month; | |
I_Rem_Params( | |
{required this.adminID, required this.companyID, required this.month}); | |
} | |
final iRemReportsFamily = | |
FutureProvider.family<double, I_Rem_Params>((ref, iremparams) async { | |
final db = FirebaseFirestore.instance; | |
final admins = db.collection("Admins"); | |
final admin = admins.doc(iremparams.adminID); | |
final companies = admin.collection("Companies"); | |
final company = companies.doc(iremparams.companyID); | |
final imports = company.collection("Imports"); | |
final getImports = await imports.get(); | |
print( | |
"provider daki değer : " + getImports.docs.length.toDouble().toString()); | |
return getImports.docs.length.toDouble(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment