Get value and set in bottom navigation in Flutter - flutter

I need to get a cart value and add as a badge in Flutter bottom navigation but value is not getting set after the function
My code
void _getCartValue() {
getCartcount().then((value) => {print(value), cartlength = value});
// setState(() {});
}
_getCartValue();
Bottom navigation code
BottomNavigationBarItem(
label: 'Cart',
icon: Badge(
shape: BadgeShape.circle,
borderRadius: BorderRadius.circular(100),
child: Icon(Icons.shopping_cart),
badgeContent: Container(
height: 15,
width: 15,
decoration:
BoxDecoration(shape: BoxShape.circle, color: Colors.red),
child: Text(
cartlength.toString(),
style: TextStyle(
color: Colors.white,
fontSize: 10,
fontWeight: FontWeight.bold),
),
),
),
),
The value is printing inside the function but I'm unable to get the value below the function. Help me to pass the value from _getCartValue to cartlength value
I'm getting null below the function

You cannot change external variable value inside the async execution body ..then, ..whenComple, ..catchError
Maybe you can try this
void _getCartValue() async {
try {
final value = await getCartCount();
cartlength = value;
// setState(() {});
} catch (_) {}
}
_getCartValue();
Edit
A clean approach is to wrap your Text widget with FutureBuilder like this
FutureBuilder(
future: getCartCount,
builder: (context, snapshot) {
return ConnectionState.done == snapshot.connectionState ? Text(
snapshot.data.toString(),
style: TextStyle(
color: Colors.white,
fontSize: 10,
fontWeight: FontWeight.bold),
) : SizedBox(width: 10.0, height: 10.0, child: CircularProgressIndicator());
},
),

Related

List of text in one Card using ListView.builder twice and for loop?

With my current code, my list is put into separate cards. I need it to be in one. I'm using two ListView.builders and a loop. I believe that's what's causing the problem. Please tell me if I'm wrong.
Every time the user taps submit on my second TextField a new TextField appears, functionality I would like to keep one way or another.
I'm just staring out so any help if appreciated.
My end goal is to have a bullet point list but the bullet points aren't important right now.
Here's my code:
class PostNote extends StatefulWidget {
User user;
PostNote({
required this.user,
});
#override
State<PostNote> createState() => _PostNoteState();
}
class _PostNoteState extends State<PostNote> {
FirebaseFirestore firestore = FirebaseFirestore.instance;
TextEditingController titleController = TextEditingController();
final List<TextField> _textFields = [];
final List<TextEditingController> _controllers = [];
bool loading = false;
#override
void initState() {
super.initState();
_addTextField();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF162242),
elevation: 0,
),
body: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(20),
child: Column(
children: [
Text(
"Title",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
SizedBox(
height: 15,
),
Container(
height: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
color: Colors.white,
),
padding: EdgeInsets.only(left: 10, right: 10),
child: TextField(
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
),
),
textInputAction: TextInputAction.next,
style: TextStyle(
color: Color(0xFF192A4F),
fontSize: 18,
),
controller: titleController,
autofocus: true,
),
),
SizedBox(
height: 30,
),
Text(
"Notes",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
SizedBox(
height: 15,
),
Container(
padding: EdgeInsets.only(left: 10, right: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
color: Colors.white,
),
child: ListView.builder( // HERE
shrinkWrap: true,
itemCount: _textFields.length,
itemBuilder: (_, index) {
return _textFields[index];
},
),
),
SizedBox(
height: 50,
),
loading
? Center(
child: CircularProgressIndicator(),
)
: Container(
height: 50,
width: MediaQuery.of(context).size.width,
child: ElevatedButton(
child: Text(
"Add Note",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Color(0xFF162242)),
shape: MaterialStateProperty.all<
RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
),
onPressed: () async {
for (var notesController in _controllers) // HERE {
if (titleController.text == "" ||
notesController.text.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:
Text("All fields are required")));
} else {
setState(() {
loading = true;
});
await FirestoreServiceEdit().insertNote(
titleController.text,
notesController.text,
widget.user.uid);
CollectionReference notes =
firestore.collection('notes');
QuerySnapshot allResults = await notes.get();
allResults.docs.forEach((DocumentSnapshot result) {
print(result.data());
});
if (!mounted) return;
setState(() {
loading = false;
});
Navigator.pop(context);
}
}
}),
),
]),
),
),
),
);
}
void _addTextField() {
final notesController = TextEditingController();
_textFields.add(
TextField(
decoration: InputDecoration(
prefix: Icon(
Icons.circle,
size: 8,
color: Colors.black,
),
),
autofocus: true,
controller: notesController,
onSubmitted: (_) => setState(() => _addTextField()),
),
);
_controllers.add(notesController);
}
}
class FirestoreServiceEdit{
FirebaseFirestore firestore = FirebaseFirestore.instance;
Future insertNote(String title, String notes, String? userId,)async{
try{
await firestore.collection('notes').add({
"title":title,
"notes":notes,
"userId":userId
});
} catch (e) {}
}
}
class NoteModelEdit {
String id;
String title;
String notes;
String userId;
NoteModelEdit({
required this.id,
required this.title,
required this.notes,
required this.userId
});
factory NoteModelEdit.fromJson(DocumentSnapshot snapshot){
return NoteModelEdit(
id: snapshot.id,
title: snapshot['title'],
notes: snapshot['notes'],
userId: snapshot['userId']
);
}
}
Home screen:
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection("notes")
.where('userId', isEqualTo: user.uid)
.snapshots(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
if (snapshot.data.docs.length > 0) {
return ListView.builder( // HERE
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
NoteModelEdit note =
NoteModelEdit.fromJson(snapshot.data.docs[index]);
return Card(
margin: EdgeInsets.only(top: 18, left: 15, right: 15),
child: Column(children: [
ListTile(
title: Center(
child: Container(
padding: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(width: 0.5),
),
),
child: Text(
note.title,
textWidthBasis: TextWidthBasis.longestLine,
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.w600),
textAlign: TextAlign.center,
),
),
),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => EditNoteScreen(),
));
},
),
SizedBox(
height: 15,
),
Align(
alignment: Alignment.centerLeft,
child: Text(
note.notes,
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.w700),
),
),
SizedBox(
height: 15,
),
]),
);
});
Thank you for your time!
You will need to augment your logic to look at all the TextFields at the same time rather than one at a time and inserting a note for each.
if (titleController.text.isEmpty ||
_controllers.any((element) => element.text.isEmpty)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:
Text("All fields are required")));
} else {
setState(() {
loading = true;
});
}
await FirestoreServiceEdit().insertNote(
titleController.text,
_controllers.map((element) => element.text).join("\n"),
widget.user.uid);
you can do like this :
Widget listInCard() {
List testList = ["note1", "note2", "note3"];
return Card(
color: Colors.blue.shade200,
elevation: 5,
child: ListView.builder(
itemCount: testList.length,
itemBuilder: (context, index) {
return Text(testList[index]);
}),
);
}
The blue color is the Card and inside it, it is a list of notes.
You can also use Container instead of Card.

Exception has occurred. FlutterError (setState() called after dispose(): _MyProfileState#c3ad1(lifecycle state: defunct, not mounted)

This Error Showing When i Click On Notification Navigation Item from Profile Page To Notification
Exception has occurred. FlutterError (setState() called after
dispose(): _MyProfileState#c3ad1(lifecycle state: defunct, not
mounted) This error happens if you call setState() on a State object
for a widget that no longer appears in the widget tree (e.g., whose
parent widget no longer includes the widget in its build). This error
can occur when code calls setState() from a timer or an animation
callback. The preferred solution is to cancel the timer or stop
listening to the animation in the dispose() callback. Another solution
is to check the "mounted" property of this object before calling
setState() to ensure the object is still in the tree. This error might
indicate a memory leak if setState() is being called because another
object is retaining a reference to this State object after it has been
removed from the tree. To avoid memory leaks, consider breaking the
reference to this object during dispose().)
Profile Page :
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:play_big_win/constants.dart';
import 'package:play_big_win/services/firebase-auth-helper.dart';
class MyProfile extends StatefulWidget {
#override
_MyProfileState createState() => _MyProfileState();
}
class _MyProfileState extends State<MyProfile> {
final firebaseUser = FirebaseAuth.instance.currentUser;
final db = FirebaseFirestore.instance;
String fullname = '';
String email = '';
#override
void dispose() {
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}
#override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}
getData() async {
while (mounted) {
dynamic names = await FirebaseAuthHelper().getCurrentUserData();
if (names != null) {
fullname = names[0];
email = names[1];
setState(() {});
} else {
print("Nulllll");
}
}
}
#override
Widget build(BuildContext context) {
getData();
return Scaffold(
backgroundColor: kPrimaryColor,
body: SafeArea(
child: ListView(
shrinkWrap: true,
children: [
SizedBox(
height: 20,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(15),
),
),
width: MediaQuery.of(context).size.width - 25,
height: 150,
child: Row(
children: [
CircleAvatar(
radius: 60.0,
backgroundImage: NetworkImage(
'https://res.cloudinary.com/dqunmzmqo/image/upload/v1606248032/male-clipart-avatar_ewaerc.png',
),
),
Padding(
padding: const EdgeInsets.only(top: 45.0),
child: Column(
children: [
Text(
"Name: $fullname",
style: TextStyle(
fontSize: 14,
),
),
SizedBox(
height: 10,
),
Text(
"Email: $email",
style: TextStyle(
fontSize: 14,
),
),
SizedBox(
height: 10,
),
Text(
"Refer Code: Xasdsas",
style: TextStyle(
fontSize: 14,
),
)
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
'Play Big Win Big',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 20.0,
color: Colors.white,
),
),
),
SizedBox(
height: 5,
),
Text(
'V1.0',
style: TextStyle(
fontSize: 10.0,
letterSpacing: 2.5,
color: Colors.teal.shade100,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 20.0,
width: 150.0,
child: Divider(
color: Colors.teal.shade100,
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => MyprivacyPolicy(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.group_add,
color: Colors.teal,
),
title: Text(
'Invite Friends',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => MyprivacyPolicy(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.question_answer,
color: Colors.teal,
),
title: Text(
"FAQ's",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => MyprivacyPolicy(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.privacy_tip,
color: Colors.teal,
),
title: Text(
'Privacy Policy',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => TermsandCondtions(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.auto_fix_normal,
color: Colors.teal,
),
title: Text(
'Terms & Conditions',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
],
),
],
),
),
);
}
}
Notification Page:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../constants.dart';
class MyNotification extends StatefulWidget {
#override
_MyNotificationState createState() => _MyNotificationState();
}
class _MyNotificationState extends State<MyNotification> {
#override
void dispose() {
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}
#override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: kPrimaryColor,
title: Text(
"Notifications",
),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Container(
decoration: BoxDecoration(
color: Colors.deepPurple,
borderRadius: BorderRadius.all(
Radius.circular(
20,
),
),
),
height: 300,
width: MediaQuery.of(context).size.width,
child: Center(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
"Today Is Your sadsadasdas das das dsa sa dsa dsad asd as das asd sad asd asds das sa dsa",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
),
);
}
}
My Authenticate Code:
Future getCurrentUserData() async {
try {
DocumentSnapshot ds = await users.doc(firebaseUser.uid).get();
String fullname = ds.get('full_name');
String email = ds.get('email');
return [fullname, email];
} catch (e) {
print(e.toString());
return null;
}
}
This Code Which I Call in Profile Page:
getData() async {
while (mounted) {
dynamic names = await FirebaseAuthHelper().getCurrentUserData();
if (names != null) {
fullname = names[0];
email = names[1];
setState(() {});
} else {
print("Null");
}
}
}
You are using setState in async function, After an await, your widget may not be mounted anymore. Doing setState gives you an exception at that time. use the below code or place it at any other place
if (this.mounted) {
setState(() {
});
}
or more clear approach override setState
#override
void setState(fn) {
if(mounted) {
super.setState(fn);
}
}

how to activate and deactivate favorite icon in flutter

I want to activate and deactivate favorite icon for every individual item
as what I am getting now is to fill that icon but at the same time,it doesnt get
deactivated.
bool isPressed=false;
new GestureDetector(
onTap: () {
setState(() => isPressed = true);
},
child: Icon(Icons.favorite_sharp,
// color: Colors.redAccent,
color: (isPressed)
? Colors.red
: Colors.black12)),
Now activation and deactivation is working but while selecting an individual favorite icon, it is showing all the favorite icon as selected.
ListView.builder(
itemCount: infoList.length,
itemBuilder: (BuildContext context, int index) {
Info info = new Info(
"${infoList[index].id}",
"${infoList[index].name}",
"${infoList[index].image}",
"${infoList[index].thumb}",
"${infoList[index].catagory}",
"${infoList[index].price}",
"${infoList[index].qty}",
);
return new Card(
margin: EdgeInsets.fromLTRB(5, 0, 5, 5),
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
child: ClipRRect(
child: ListTile(
leading: Container(
child: Image.network("${infoList[index].image}")),
title: Text(
"${infoList[index].name}",
style: TextStyle(
fontStyle: FontStyle.normal,
fontSize: 14,
color: Colors.black),
),
subtitle: Text(
"\$ ${infoList[index].price}",
style: TextStyle(
fontStyle: FontStyle.normal,
fontSize: 14,
color: Colors.black),
),
trailing: Wrap(
spacing: 12,
children: [
GestureDetector(
onTap: () {
setState(() => isPressed = !isPressed);
},
child: Icon(Icons.favorite_sharp,
// color: Colors.redAccent,
color: (isPressed)
? Colors.red
: Colors.black12)),
// Icon(
// Icons.add_shopping_cart,
// color: Colors.white,
// ),
],
),
You need to change your onTap to actually toggle:
onTap: () {
setState(() => isPressed = !isPressed);
},
I just added an ' _ ' to the isPressed variable as it is a good practice to keep such variables as private.
i.e.
isPressed -> Public
_isPressed -> Private
bool _isPressed = false;
GestureDetector(
onTap: () {
setState(() => _isPressed = !_isPressed);
},
child: Icon(Icons.favorite_sharp,
color: _isPressed ? Colors.red : Colors.black12)),
For each widget to have it's on selection you'll have to make it into a separate Stateful Widget and then pass it into the ListView.
Once you've done that now every widget will have it's own state and they can be separately selected / disselected.
EDIT
Make the Card Widget into another StateFull widget
For eg.
class CardWidget extends StatefulWidget {
final Info info;
CardWidget(this.info);
#override
_CardWidgetState createState() => _CardWidgetState();
}
class _CardWidgetState extends State<CardWidget> {
bool _isPressed = false;
#override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.fromLTRB(5, 0, 5, 5),
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
child: ClipRRect(
child: ListTile(
leading: Container(
child: Image.network("${widget.info.image}")),
title: Text(
"${widget.info.name}",
style: TextStyle(
fontStyle: FontStyle.normal,
fontSize: 14,
color: Colors.black),
),
subtitle: Text(
"\$ ${widget.info.price}",
style: TextStyle(
fontStyle: FontStyle.normal,
fontSize: 14,
color: Colors.black),
),
trailing: Wrap(
spacing: 12,
children: [
GestureDetector(
onTap: () {
setState(() => _isPressed = !_isPressed);
},
child: Icon(Icons.favorite_sharp,
// color: Colors.redAccent,
color: (isPressed)
? Colors.red
: Colors.black12)),
// Icon(
// Icons.add_shopping_cart,
// color: Colors.white,
// ),
],
),
Now use the CardWidget to populate the ListView.
Just remember to pass in all the values to the widget as shown below.
ListView.builder(
itemCount: infoList.length,
itemBuilder: (BuildContext context, int index) {
Info info = new Info(
"${infoList[index].id}",
"${infoList[index].name}",
"${infoList[index].image}",
"${infoList[index].thumb}",
"${infoList[index].catagory}",
"${infoList[index].price}",
"${infoList[index].qty}",
);
return CardWidget(info);

Flutter Provider.of and search

I am a beginner in flutter and app development. I have a problem. I am using Provider.of in order to get my data. I am getting data and showing it in ListView.builder with no problem. But I want to make a search on my list.
Please refer to code below
class RecipeList extends StatefulWidget {
#override
_RecipeListState createState() => _RecipeListState();
}
class _RecipeListState extends State<RecipeList> {
List<Recipe>showList =List();//creating my list of searched data
#override
Widget build(BuildContext context) {
//getting my recipe list in order to show them
final recipes = Provider.of<List<Recipe>>(context);
showList=recipes;
final user = Provider.of<User>(context);
String _image;
Widget myImage(int index,)
{
if(recipes[index].image == ''){
return Image.asset('images/no_image.jpg');
}
else{
return
FadeInImage.assetNetwork(
width: 300,
height: 250,
placeholder: 'images/loading.webp',
image: recipes[index].image,
);
}
}
return StreamBuilder<UserData>(
stream:DatabaseService(uid: user.uid).userData,
builder: (context,snapshot){
if(snapshot.hasData) {
UserData userdata = snapshot.data;
if (userdata.is_admin == true) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [Colors.blue[200], Colors.orange[100]])),
child: Scaffold(
appBar: AppBar(
title: Text('Recipes'),
backgroundColor: Colors.transparent,
elevation: 0,
),
backgroundColor: Colors.transparent,
body: Column(
children: <Widget>[
Material(
elevation: 0,
color: Colors.transparent,
child: TextField(
onChanged: (val) {
val = val.toLowerCase();
setState(() {
showList = recipes.where((recipe){
var title = recipe.name.toLowerCase();
return title.contains(val);
}).toList();
});
},
decoration: InputDecoration(
labelText: "Search",
hintText: "Search",
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(25.0)))),
),),
SizedBox(height: 15,),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: showList.length,
itemBuilder: (context, index) {
if (recipes[index].image == null) {
String _image = 'images/new.png';
}
else {
_image = recipes[index].image;
}
// print(recipes[index].image);
return Column(
children: <Widget>[
SlimyCard(
color: Colors.teal[200],
width: 300,
topCardHeight: 350,
bottomCardHeight: 300,
borderRadius: 15,
topCardWidget: Column(
children: <Widget>[
Text(recipes[index].name[0]
.toUpperCase() +
recipes[index].name.substring(1),
style: TextStyle(
fontSize: 35,
color: Colors.white,
fontWeight: FontWeight.bold,
),),
ClipRRect(borderRadius: BorderRadius
.circular(25.0),
child: myImage(index)
),
// Image.network('https://www.bbcgoodfood.com/sites/default/files/recipe-collections/collection-image/2013/05/chorizo-mozarella-gnocchi-bake-cropped.jpg')),
],
),
bottomCardWidget: SingleChildScrollView(
child: Column(
children: <Widget>[
Text('Ingredients',
style: TextStyle(
fontSize: 25,
color: Colors.white
),),
SizedBox(height: 5,),
Text(recipes[index].ingredients,
style: TextStyle(
fontSize: 16
),),
SizedBox(height: 20,),
Text('Recipe',
style: TextStyle(
fontSize: 25
,
color: Colors.white
),),
SizedBox(height: 5,),
Text(recipes[index].recipe,
style: TextStyle(
fontSize: 16
),),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius
.circular(7.0),
//side: BorderSide(color: Colors.orange)
),
color: Color.fromRGBO(
233, 217, 108, 1),
onPressed: () async {
final CollectionReference recipecollection = Firestore
.instance.collection(
'recipe');
await recipecollection.document(
recipes[index].id).delete();
StorageReference firestoreStorageref = await FirebaseStorage
.instance
.getReferenceFromUrl(
recipes[index].image);
firestoreStorageref.delete();
},
child: Text(
'Delete'
),
)
],
),
),
slimeEnabled: false,
),
SizedBox(height: 25,)
],
);
},
)),
],
)
),
);
}
I want to show this list on the search and modify it. first I fill it with data from the provider.
I have created a TextField for Search the onChanged method filters the typed value and returns a list. When I print in onChanged function it is working.
I am showing my list with ListView, when I print the size of showList in onChanged function, it filters and gives the right value but when I use it for itemCount it never changes
You can use searchable_dropdown instead of the TextField. You can assign the list to it and it will search the list based on the to string method so you have to override it.
Refer the link to the dependency: https://pub.dev/packages/searchable_dropdown.

flutter: error during use shared preference

I'm trying to use shared preference to keep user login and I put it in the splash screen and when I run the project this error has appeared:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception:
setState() called after dispose(): _SplashScreenState#aa9c8(lifecycle
state: defunct) This error happens if you call setState() on a State
object for a widget that no longer appears in the widget tree (e.g.,
whose parent widget no longer includes the widget in its build). This
error can occur when code calls setState() from a timer or an
animation callback. The preferred solution is to cancel the timer or
stop listening to the animation in the dispose() callback. Another
solution is to check the "mounted" property of this object before
calling setState() to ensure the object is still in the tree. This
error might indicate a memory leak if setState() is being called
because another object is retaining a reference to this State object
after it has been removed from the tree. To avoid memory leaks,
consider breaking the reference to this object during dispose().
And the data loop doesn't stop calling itself again and again.
This my method to using shared preference:
bool isLoading = true;
init() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isLog = prefs.getBool("islog");
if (isLog == true) {
String email = prefs.getString("email");
String pass = prefs.getString("pass");
setState(() {
signIn(email, pass);
});
} else {
setState(() {
isLoading = false;
});
}
}
signIn(String email, String pass) async {
var res = await userProvider.login(email, pass);
var user = userProvider.user.tourist;
if (res is FailedRequest) {
Dialogs.showErrorDialog(context, message: res.message, code: res.code);
} else if (user == true) {
print("Signing in success");
await appProvider.countryList();
setState(() {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => BottomScreen()));
});
}
userProvider.isLoading = false;
setState(() {
isLoading = false;
});
}
this is my splash screen which contain the previous code of shared preference
#override
Widget build(BuildContext context) {
userProvider = Provider.of<UserProvider>(context, listen: false);
appProvider = Provider.of<AppProvider>(context, listen: false);
init();
return isLoading == true
? Container(
color: Colors.white,
child: Center(
child: CircularProgressIndicator()),
)
: Container(
child: Scaffold(
body: Stack(
children: <Widget>[
Container(
foregroundDecoration: !AppTheme.isLightTheme
? BoxDecoration(
color: AppTheme.getTheme()
.backgroundColor
.withOpacity(0.4))
: null,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Image.asset('assets/images/introduction.jpg',
fit: BoxFit.cover),
),
Column(
children: <Widget>[
Expanded(
flex: 1,
child: SizedBox(),
),
Center(
child: Container(
width: 60,
height: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(8.0),
),
boxShadow: <BoxShadow>[
BoxShadow(
color: AppTheme.getTheme().dividerColor,
offset: Offset(1.1, 1.1),
blurRadius: 10.0),
],
),
child: ClipRRect(
borderRadius: BorderRadius.all(
Radius.circular(8.0),
),
child: Image.asset('assets/images/appIcon.png'),
),
),
),
SizedBox(
height: 16,
),
Text(
"Voyager",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 24,
),
),
SizedBox(
height: 8,
),
Text(
"Best Trips deals for your holiday",
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 14,
),
),
Expanded(
flex: 4,
child: SizedBox(),
),
Padding(
padding: const EdgeInsets.only(
left: 48, right: 48, bottom: 8, top: 8),
child: Container(
height: 48,
decoration: BoxDecoration(
color: AppTheme.getTheme().primaryColor,
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: AppTheme.getTheme().dividerColor,
blurRadius: 8,
offset: Offset(4, 4),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
highlightColor: Colors.transparent,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
IntroductionScreen()),
);
},
child: Center(
child: Text(
"Get started",
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 16,
color: Colors.white),
),
),
),
),
),
),
Padding(
padding: EdgeInsets.only(
bottom:
24.0 + MediaQuery.of(context).padding.bottom,
top: 16),
child: Container(
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
highlightColor: Colors.transparent,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LoginScreen(context)),
);
},
child: Text(
"Already have account? LogIn",
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 14,
color: Colors.white,
),
),
),
),
),
),
],
),
],
),
),
);
}
So can anyone help me please with my issue?
Pls, call your init function in microtask.
Future.microtask(() => {init()});
#Mariam please use mounted,
if (this.mounted){
setState((){
//Your state change code goes here
});
}
use mounted before setState
if (isLog) {
String email = prefs.getString("email");
String pass = prefs.getString("pass");
if(mounted)
setState(() {
signIn(email, pass);
});
} else {
if(mounted)
setState(() {
isLoading = false;
});
}