Adding a condition to retrieve certain data? - firebase

I want to add a conditional expression to the code so that the data that has the field 'category' entered as BS in firebase realtime database will be called and displayed. Here is a picture of the database table : https://imgur.com/a/PkdI71d
How do I add an expression to the following code so that only "English for Career Development" will be displayed and not "Excel Skills for Business" since "English for Career Development", or test 4 has a field category = 'bs' but test 5 doesn't so it will not show up.
Code:
class _BusinessPage1State extends State<BusinessPage1> {
List<AllCourses> coursesList = [];
#override
void initState(){
super.initState();
DatabaseReference referenceAllCourses = FirebaseDatabase.instance.reference().child('AllCourses');
referenceAllCourses.once().then(((DataSnapshot dataSnapshot){
coursesList.clear();
var keys = dataSnapshot.value.keys;
var values = dataSnapshot.value;
for(var key in keys){
AllCourses allCourses = new AllCourses(
values [key]["courseName"],
values [key]["teacher"],
values [key]["category"],
);
coursesList.add(allCourses);
}
setState(() {
//
});
}));
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: ()
{Navigator.pop(context);
Navigator.push(context, MaterialPageRoute(builder: (context)=>homepage()));}),
title: Text("Creator's Club"),
backgroundColor: Color(0xff2657ce),
elevation: 0,),
body: Container(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Business', style: TextStyle(
color: Color(0xff2657ce),
fontSize: 27,
),),
Text('Choose which course you want to study.', style: TextStyle(
color: Colors.black.withOpacity(0.6),
fontSize: 20
),),
SizedBox(height: 10),
Expanded(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
coursesList.length == 0 ? Center(child: Text("Loading...", style: TextStyle(fontSize: 15),)): ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: coursesList.length,
itemBuilder: (_, index) {
return CardUI(coursesList[index].courseName, coursesList[index].teacher, coursesList[index].category);
}
)
]
),
),
),
]
)
)
);
}
}
Widget CardUI (String courseName, String teacher, String category){
return Card(
elevation: 1,
margin: EdgeInsets.all(5),
color: Color(0xffd3defa),
child: Container(
color: Colors.white,
margin: EdgeInsets.all(1),
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Color(0xffd3defa),
borderRadius: BorderRadius.all(Radius.circular(17)),
),
child: IconButton(
icon: Icon(
Icons.star_border_rounded ,
color: Color(0xff2657ce),
),
),
),
SizedBox(width: 15,),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: InkWell(
onTap: (){},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(courseName, style: TextStyle(fontSize: 18)),
SizedBox(height: 5),
Text(teacher, style: TextStyle(fontSize: 15, color: Colors.grey)),
SizedBox(height: 5),
Text(category, style: TextStyle(fontSize: 15)),
],
),
)
)
]
)
]
)
],
)
),
);
}
Code for 'AllCourses' :
class AllCourses {
String courseName;
String teacher;
String category;
AllCourses(this.courseName, this.teacher, this.category);
}

Just add a condition to check if the category is 'bs' before adding the course to the coursesList:
for(var key in keys){
AllCourses allCourses = new AllCourses(
values [key]["courseName"],
values [key]["teacher"],
values [key]["category"],
);
if(allCourses.get('category') == 'bs')
coursesList.add(allCourses);
}

Related

How to show 3 text widgets inside a list-view-builder?

Im trying to show a listviewbuilder with 3 text widgets. But the last text widget don't looks good. Heres how it looks
And heres my code
#override
Widget build(BuildContext context) {
final user = Provider.of<Userforid>(context);
if (nosuerfound == true) {
return ListView.builder(
itemCount: _resultsList.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
child: ListTile(
onTap: () {
DatbaseService.instance
.createorGetConversation(user.uid, _resultsList[index].id,
(String _conversationID) {
/* NavigationService.instance.navigateToRoute(
MaterialPageRoute(builder: (context) {
return MeineBeitraege(
_conversationID,
_resultsList[index].id,
_resultsList[index].data()['username'],
);
}),
);*/
});
},
leading: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: Container(
child: Text(
_resultsList[index].data()['hashtag1'],
style: const TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20.0,
),
),
),
),
Expanded(
child: Container(
child: Text(
_resultsList[index].data()['hashtag2'],
style: const TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20.0,
),
),
),
),
Expanded(
child: Container(
child: Text(
_resultsList[index].data()['hashtag3'],
style: const TextStyle(
// fontWeight: FontWeight.w500,
fontSize: 20.0,
),
),
),
),
],
),
// subtitle: Text(_resultsList[index].data()['email']),
),
);
});
} else {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 30, 0, 0),
child: Container(
child: Text(
"No Hashtag found",
style: TextStyle(fontSize: 16),
)),
);
}
}
}
So what I want is getting a bit padding between every text widget inside column. And also the last hashtag should be showed correctly . Not showed half .Hope anyone can help .if you need more informations please leave a comment .
You don't need use ListTile for show three elements in trailing, use custom widget or simple Container with Gesture Detector (or InkWell for the material tap effect).
It is not necessary either the Expanded Widget.
ListView.builder(
itemCount: _resultsList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
margin: const EdgeInsets.all(10.0), // Add margin
child: InkWell(
onTap: () {
/*DatbaseService.instance
.createorGetConversation(user.uid, _resultsList[index].id,
(String _conversationID) {
/* NavigationService.instance.navigateToRoute(
MaterialPageRoute(builder: (context) {
return MeineBeitraege(
_conversationID,
_resultsList[index].id,
_resultsList[index].data()['username'],
);
}),
);*/
});*/
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_resultsList[index].data()['hashtag1'],
'Text 1',
style: const TextStyle(
fontWeight: FontWeight.w500,
fontSize: 15.0,
),
),
Text(
_resultsList[index].data()['hashtag2'],
style: const TextStyle(
fontWeight: FontWeight.w500,
fontSize: 15.0,
),
),
Text(
_resultsList[index].data()['hashtag3'],
style: const TextStyle(
// fontWeight: FontWeight.w500,
fontSize: 15.0,
),
),
],
),
),
);
},
),

Retrieve same key data from Firebase on all pages

So the problem is I'm trying to retrieve this data from the Firebase database, where each case study
represent one key of data, and what I get is the same data of the first key on all the case study pages.
Data on the Firebase:
List of Case studies Page:
enter image description here
Case Study Page:
These are two different case studies yet have the same data.
enter image description here
enter image description here
I used Animated List at the beginning to retrieve the data but it gave me the same problem, so I
followed a Youtube Tutorial and used another way but the problem remains. I know I need to pass a key when the button is clicked to link each case study with its key but I'm not sure how.
Here is the code for more clarification:
stu_case_study_view.dart
Case Study Page
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'package:flutter/material.dart';
import 'package:virtulab/Model/stu_case_study_model.dart';
import 'package:virtulab/functions/database.dart';
import 'package:virtulab/widgets/back_button.dart';
class CaseStudyView extends StatefulWidget {
final String csKey;
CaseStudyView({this.csKey});
#override
State<StatefulWidget> createState() {
return _CaseStudyView();
}
}
class _CaseStudyView extends State<CaseStudyView> {
bool _loading = false;
List<CaseStudyModel> caseStudyList = [];
final _formKey = GlobalKey<FormState>();
final question1Controller = TextEditingController();
final question2Controller = TextEditingController();
final question3Controller = TextEditingController();
final question4Controller = TextEditingController();
final question5Controller = TextEditingController();
DatabaseReference caseStudyInfo;
#override
void initState() {
super.initState();
caseStudyInfo = firebaseref.child('case_study');
caseStudyInfo.once().then((DataSnapshot snap) {
var keys = snap.value.keys;
var data = snap.value;
caseStudyList.clear();
for (var key in keys) {
CaseStudyModel csList = new CaseStudyModel(
data[key]['title'],
data[key]['description'],
data[key]['body'],
data[key]['question1'],
data[key]['question2'],
data[key]['question3'],
data[key]['question4'],
data[key]['question5'],
);
caseStudyList.add(csList);
}
setState(() {
print('Length : $caseStudyList.length');
});
});
}
#override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Case Study'), //Temp data
backgroundColor: Colors.deepPurple,
),
body: Container(
child: caseStudyList.length == 0
? new Text('No Case Studies Uploaded')
: new ListView.builder(
itemCount: caseStudyList.length,
itemBuilder: (_, index) {
return _caseStudy(
caseStudyList[index].title,
caseStudyList[index].description,
caseStudyList[index].body,
caseStudyList[index].question1,
caseStudyList[index].question2,
caseStudyList[index].question3,
caseStudyList[index].question4,
caseStudyList[index].question5,
);
}),
),
);
}
_showAlertDialog(BuildContext context) {
Widget cancelButton = FlatButton(
child: Text('Cancel'),
onPressed: () => Navigator.of(context, rootNavigator: true).pop());
Widget submitButton = FlatButton(
child: Text('Submit'),
onPressed: () {
if (_formKey.currentState.validate()) {
firebaseref.child('case_study').push().set({
"question1": question1Controller.text,
"question2": question2Controller.text,
"question3": question3Controller.text,
"question4": question4Controller.text,
"question5": question5Controller.text,
});
Navigator.of(context, rootNavigator: true).pop();
Navigator.pop(context);
}
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text('Alert'),
content: Text('Are you sure you want to submit?'),
actions: [
cancelButton,
submitButton,
],
);
// show the dialog
showDialog(
context: context,
// barrierDismissible: false,
builder: (BuildContext cxt) {
return alert;
},
);
}
Widget _caseStudy(
String title,
String description,
String body,
String question1,
String question2,
String question3,
String question4,
String question5) {
return _loading
? Container(
child: Center(
child: CircularProgressIndicator(),
),
)
: Container(
padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
child: Form(
key: _formKey,
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 20,
vertical: 20,
),
child: ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
height: 2,
color: Colors.deepPurple),
),
Divider(),
Text(
'Description:',
textAlign: TextAlign.left,
style: TextStyle(fontWeight: FontWeight.bold),
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Text(
description,
textAlign: TextAlign.left,
maxLines: null,
),
),
Text(
'Case Study:',
textAlign: TextAlign.left,
style: TextStyle(fontWeight: FontWeight.bold),
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Text(
body,
textAlign: TextAlign.left,
maxLines: null,
),
),
Divider(),
Text(
'Questions:',
textAlign: TextAlign.left,
style: TextStyle(fontWeight: FontWeight.bold),
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'1/ ' + question1,
textAlign: TextAlign.left,
maxLines: null,
),
],
)),
TextFormField(
controller: question1Controller,
validator: (v) =>
v.isEmpty ? 'Enter Your Answer' : null,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Write Your Answer Here',
),
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'2/ ' + question2,
textAlign: TextAlign.left,
maxLines: null,
),
],
)),
TextFormField(
controller: question2Controller,
validator: (v) =>
v.isEmpty ? 'Enter Your Answer' : null,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Write Your Answer Here',
),
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'3/ ' + question3,
textAlign: TextAlign.left,
maxLines: null,
),
],
)),
TextFormField(
controller: question3Controller,
validator: (v) =>
v.isEmpty ? 'Enter Your Answer' : null,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Write Your Answer Here',
),
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'4/ ' + question4,
textAlign: TextAlign.left,
maxLines: null,
),
],
)),
TextFormField(
controller: question4Controller,
validator: (v) =>
v.isEmpty ? 'Enter Your Answer' : null,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Write Your Answer Here',
),
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'5/ ' + question5,
textAlign: TextAlign.left,
maxLines: null,
),
],
)),
TextFormField(
controller: question5Controller,
validator: (v) =>
v.isEmpty ? 'Enter Your Answer' : null,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Write Your Answer Here',
),
),
Align(
alignment: Alignment.centerRight,
child: Container(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: <Widget>[
FloatingActionButton.extended(
onPressed: () {
_showAlertDialog(context);
},
label: Text(
"Submit",
style: TextStyle(
fontWeight: FontWeight.bold),
),
backgroundColor: Colors.amber,
),
],
))),
),
]),
)));
}
}
stu_case_study_model.dart
class CaseStudyModel {
String title,
description,
body,
question1,
question2,
question3,
question4,
question5;
CaseStudyModel(this.title, this.description, this.body, this.question1,
this.question2, this.question3, this.question4, this.question5);
}
stu_caseStudies_list.dart
List of Case studies Page
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'package:flutter/material.dart';
import 'package:virtulab/functions/Student/class_case_study_list.dart';
import 'package:virtulab/functions/database.dart';
import 'package:virtulab/student/stu_case_study_view.dart';
class CaseStudiesList extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _CaseStudiesList();
}
}
class _CaseStudiesList extends State<CaseStudiesList> {
Query _caseStudyTitle;
#override
void initState() {
super.initState();
_caseStudyTitle = firebaseref.child('case_study');
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Case Studies'),
backgroundColor: Colors.deepPurple,
),
body: FirebaseAnimatedList(
query: _caseStudyTitle,
defaultChild: Center(child: CircularProgressIndicator()),
itemBuilder: (BuildContext context, snapshot,
Animation<double> animation, int index) {
Map caseStudy = snapshot.value;
caseStudy['key'] = snapshot.key;
return _caseStudyList(caseStudy: caseStudy);
},
),
);
}
Widget _caseStudyList({Map caseStudy}) {
return Column(
children: [
Card(
child: InkWell(
onTap: () {},
child: Container(
margin: EdgeInsets.symmetric(vertical: 10),
// padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
// height: 70,
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(
Icons.description_sharp,
),
SizedBox(width: 20),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
caseStudy['title'],
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
height: 2,
color: Colors.deepPurple),
),
],
),
],
),
ElevatedButton(
onPressed: () => {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CaseStudyView(
//csKey: caseStudy['key'],
),
),
)
},
child: Text('Start'),
// color: Colors.amber,
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
Colors.amber))),
],
),
),
SizedBox(height: 5),
],
),
),
),
),
],
);
}
}
This is my graduation project and I sincerely appreciate your help. Thank you.

flutter :sharedpreference retrieving null value [duplicate]

This question already has answers here:
What is a NoSuchMethod error and how do I fix it?
(2 answers)
Closed 2 years ago.
The method 'getStringList' was called on null.
Receiver: null
Tried calling: getStringList("userCart")
The relevant error-causing widget was:
Consumer .the details are stored in firebase,the error occurs when displaying the no of items the user have added to the cart.
the CartItemCounter dart file is that thrown error when calling consumer.initially useCart is given as garbage value when registering in firebase.
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
class StoreHome extends StatefulWidget {
#override
_StoreHomeState createState() => _StoreHomeState();
}
class _StoreHomeState extends State<StoreHome> {
SharedPreferences sharedPreferences;
#override
void initState() {
super.initState();
SharedPreferences.getInstance().then((prefs){
setState(() {
sharedPreferences=prefs;
});
});
}
#override
Widget build(BuildContext context) {
final _width=MediaQuery.of(context).size.width;
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text("E_shop",style: TextStyle(color:Colors.red,),
),
centerTitle: true,
actions: [
Stack(
children: [
IconButton(icon:Icon(Icons.add_shopping_cart,color: Colors.grey,),
onPressed: (){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>StoreHome()));
},
),
Positioned(child: Stack(
children: [
Icon(Icons.brightness_1,size: 20.0,
color: Colors.green,),
Positioned(
top: 3.0,
bottom: 4.0,
left:4.0,
child: Consumer<CartItemCounter>(
builder: (context, counter,_){
return Text(
counter.count.toString(),
style:TextStyle(color:Colors.white,fontSize:12.0,fontWeight:FontWeight.w500),
);
},),
),
],
),
),
],
),
],
),
drawer: MyDrawer(),
body: CustomScrollView(
slivers: [
SliverPersistentHeader(
pinned: true,
delegate: SearchBoxDelegate(),
),
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection("items").limit(15).orderBy("publishedDate",descending: true).snapshots(),
builder: (context,dataSnapshot){
return !dataSnapshot.hasData
?SliverToBoxAdapter(child: Center(child: circularProgress(),),)
:SliverStaggeredGrid.countBuilder(
crossAxisCount: 1,
itemCount: 5,
staggeredTileBuilder: (c)=>StaggeredTile.fit(1),
itemBuilder: (context,index){
ItemModel model=ItemModel.fromJson(dataSnapshot.data.docs[index].data());
return sourceInfo(model,context);
},
);
},
),
],
),
),
);
}
circularProgress(){
return Container(
alignment: Alignment.center,
padding: EdgeInsets.only(top: 12.0),
child: CircularProgressIndicator(valueColor: AlwaysStoppedAnimation(Colors.lightGreenAccent),),
);
}
Widget sourceInfo(ItemModel model, BuildContext context , {Color background, removeCartFunction}) {
return InkWell(
onTap: (){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>ProductPage(itemModel:model)));
},
splashColor: Colors.pink,
child: Padding(
padding: EdgeInsets.all(6.0),
child: Container(
height: 300.0,
width: MediaQuery.of(context).size.width,
child: Row(
children: [
Image.network(model.thumbnailUrl ,width:140.0,height: 140.0,),
SizedBox(width: 4.0,),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 16.0,),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(child: Text(
model?.title ?? '',style: TextStyle(color: Colors.black,fontSize: 14.0),
),
)
],
),
),
SizedBox(height: 5.0,),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(child: Text(
model?.shortInfo ?? '',style: TextStyle(color: Colors.black54,fontSize: 12.0),
),
)
],
),
),
SizedBox(height: 20.0,),
Row(
children: [
Container(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: Colors.pink,
),
alignment: Alignment.topLeft,
width: 40.0,
height: 43.0,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"50%",style: TextStyle(
fontSize: 15.0,color: Colors.white,
fontWeight: FontWeight.bold,
),
),
Text(
"OFF",style: TextStyle(
fontSize: 12.0,color: Colors.white,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
SizedBox(width: 10.0,),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(top: 0.0),
child: Row(
children: [
Text(
"Original Price :%",
style: TextStyle(
fontSize: 14.0,
color: Colors.grey
),
),
Text(
(model?.price??'').toString(),
style: TextStyle(
fontSize: 15.0,
color: Colors.grey,
decoration: TextDecoration.lineThrough,
),
)
],
),
),
Padding(
padding: EdgeInsets.only(top: 5.0),
child: Row(
children: [
Text(
"New Price :%",
style: TextStyle(
fontSize: 16.0,
color: Colors.red,
),
),
Text(
"%",
style: TextStyle(
fontSize: 15.0,
color: Colors.grey,
),
),
Text(
(model.price+model.price).toString(),
style: TextStyle(
fontSize: 15.0,
color: Colors.grey
),
),
],
),
),
],
),
],
),
Flexible(child: Container(
),
),
//to implement cart add/remove remove
Align(
alignment: Alignment.centerRight,
child: removeCartFunction==null
?IconButton(
icon: Icon(Icons.add_shopping_cart,color: Colors.pinkAccent,),
onPressed: (){
checkItemInCart(model.shortInfo,context);
},)
:IconButton(
icon: Icon(Icons.delete),
onPressed: null)
)
],
))
],
),
),
),
);
}
void checkItemInCart(String shortInfoAsId, BuildContext context) {
sharedPreferences.getStringList(EcommerceApp.userCartList).contains(shortInfoAsId)
?Fluttertoast.showToast(msg: "item already in cart")
:addItemToCart(shortInfoAsId,context);
}
addItemToCart(String shortInfoAsId, BuildContext context) {
List tempCartList=sharedPreferences.getStringList(EcommerceApp.userCartList);
tempCartList.add(shortInfoAsId);
FirebaseFirestore.instance.collection("users").doc(sharedPreferences.getString(EcommerceApp.userUID))
.update({
EcommerceApp.userCartList: tempCartList,
}).then((value){
Fluttertoast.showToast(msg: "Item added to cart successfully");
sharedPreferences.setStringList(EcommerceApp.userCartList, tempCartList);
Provider.of<CartItemCounter>(context,listen: false).displayResult();
});
}
}```
the CartItemCounter dart file is that thrown error when calling consumer<cartitemcounter>.initially useCart is given as garbage value when registering in firebase.
```class CartItemCounter extends ChangeNotifier{
static SharedPreferences sharedPreferences;
int _counter=sharedPreferences.getStringList("userCart").length-1;
int get count=>_counter;
Future<void> displayResult() async{
int _counter=sharedPreferences.getStringList("userCart").length-1;
await Future.delayed(const Duration(milliseconds: 100),(){
notifyListeners();
});
}
}```
The method 'getStringList' was called on null.
Receiver: null
Tried calling: getStringList("userCart")
The relevant error-causing widget was:
Consumer<CartItemCounter> .the details are stored in firebase,the error occurs when displaying the no of items the user have added to the cart.
The error:
getStringList("...") was called on null
implies that the method was called on a null object which means that you are trying to call sharedPrefs.getStringList()
but, sharedPrefs is null at the moment as it has not been loaded yet. Hence, you encounter the problem. A simple hack to solve this problem:
#override
Widget build(BuildContext context) {
final _width=MediaQuery.of(context).size.width;
return SafeArea(
child:
sharedPrefs == null? // If sharedPrefs is not retreived yet
Scaffold(body: Center(child: Text("Hold on :)"))): // Show this widget
Scaffold( // Else do your normal job
appBar: AppBar(
title: Text("E_shop",style: TextStyle(color:Colors.red,),
),
......... and so on..

Show Spinner using ModalProgressHUD in Flutter whilst ListView.builder reads information from Firebase

I have a screen where I try to read all my documents as a list using a Listview.builder in Flutter. I want to use ModalProgressHUD to show circular spinner whilst data is read from Firebase. When it's done building, I want to stop showing circular spinner by setting showSpinner to false.
I can hack this for a form screen but can't seem to for this view all screen, perhaps it's because it's a Listview.builder.
Thanks in advance.
Code below:
//class wide declaration
bool showSpinner = true;
Widget build(BuildContext context) {
ExpenseNotifier expenseNotifier = Provider.of<ExpenseNotifier>(context);
Future<void> _resfreshList() async {
expenseNotifier.getExpenses(expenseNotifier);
}
return Scaffold(
body: ModalProgressHUD(
inAsyncCall: showSpinner,
child: RefreshIndicator(
onRefresh: _resfreshList,
child: Consumer<ExpenseNotifier>(
builder: (context, expense, child) {
return expense == null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
PaddingClass(bodyImage: 'images/empty.png'),
SizedBox(
height: 20.0,
),
Text(
'You don\'t have any expenses',
style: kLabelTextStyle,
),
],
)
: ListView.separated(
itemBuilder: (context, int index) {
var myExpense = expense.expenseList[index];
return Card(
elevation: 8.0,
color: Colors.white70,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
RegularExpenseTextPadding(
regText:
'${_formattedDate(myExpense.updatedAt)}',
),
Container(
margin: EdgeInsets.all(20.0),
padding: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5.0)),
border: Border.all(
color: kThemeStyleBorderHighlightColour),
),
child: Row(
children: <Widget>[
Expanded(
flex: 5,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(
'${myExpense.amount}',
style: kRegularTextStyle,
),
SizedBox(
height: 20.0,
),
Text(
myExpense.description,
style: kRegularTextStyle,
),
],
),
),
Expanded(
flex: 1,
child: GestureDetector(
onTap: () {
expenseNotifier.currentExpense =
expenseNotifier
.expenseList[index];
Navigator.of(context).push(
MaterialPageRoute(builder:
(BuildContext context) {
return ExpenseDetailsScreen();
}));
},
child: Icon(
FontAwesomeIcons.caretDown,
color: kThemeIconColour,
),
),
),
],
),
),
],
),
);
},
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
height: 20.0,
);
},
itemCount: expenseNotifier.expenseList.length,
);
},
),
),
),
);
}

Iterate over firebase database nodes flutter

I want to iterate through the customers available and get the posts of every customer and show it inside a listview.
However, if i specify the database reference as final postsRef = FirebaseDatabase.instance.reference().child('Customers available').child(Uid).child('Posts').orderByChild('timestamp');, I get only the user's posts.
Below is my code for building the listview and another is my database reference. Thanks final postsRef = FirebaseDatabase.instance.reference().child('Customers available')
This is my Firebase Database Screenshot
future: postsRef.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
lists.clear();
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
return new ListView.builder(
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
return Container(
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey[200]))
),
padding: EdgeInsets.all(SizeConfig.blockSizeHorizontal * 4),
child: Row(
children: <Widget>[
Container(
height: 50,
width: 50,
child: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(150),
image: DecorationImage(image: FirebaseImage('gs://tipy-98639.appspot.com/profile_pic'))
),
),
decoration: BoxDecoration(
color: Colors.grey,
image: DecorationImage(image: AssetImage('images/dp.png')),
borderRadius: BorderRadius.circular(150)
),
),
SizedBox(width: SizeConfig.blockSizeHorizontal * 3,),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('NAME', style: TextStyle(
fontFamily: 'myriadpro',
fontWeight: FontWeight.bold,
fontSize: SizeConfig.blockSizeVertical * 1.7
),),
SizedBox(height: SizeConfig.blockSizeVertical * 1,),
Text(lists[index]["text"],
maxLines: 20,
overflow: TextOverflow.clip,
style: TextStyle(
fontFamily: 'myriadpro',
fontSize: SizeConfig.blockSizeVertical * 1.7
),),
],
),
),
],
),
);
});
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(valueColor: AlwaysStoppedAnimation(Colors.green),),
Text('Loading your posts... 🕑',
style: TextStyle(
fontFamily: 'myriadpro'
),
)
],
));
}),`

Resources