I have a counter variable that is incremented every time a user opens a page. When the page is opened for 5th time I want to show a dialog. So I have used showDialog in this case. I have used showDialog inside a function. And used it in a if else short hand. In the 5th time the app is showing the following error -
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5253 pos 12: '!_debugLocked': is not true.
Here is the code-
Future<Widget> _showDialog(BuildContext c) async {
return await showDialog(
context: c,
barrierDismissible: false,
builder: (c) {
return AlertDialog(
elevation: 24.0,
backgroundColor: Colors.blue,
title: Text(
'Liking our app?',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
actions: <Widget>[
TextButton(
child: Text(
"Continue as Guest",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
onPressed: () {},
),
TextButton(
child: Text(
"Sign Up",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
onPressed: () {},
),
],
);
});
}
Text("$_counter", textAlign: TextAlign.center),
(_counter == 5) ? _showDialog(context) : SizedBox(),
I had to add the following line of code before returning await showDialog(....);
await Future.delayed(Duration(seconds: 1));
And used Futurebuilder instead of directly calling the function.
(_counter == 8)
? FutureBuilder<Widget>(
future: _showDialog(context),
builder: (context, AsyncSnapshot<Widget> snap) {
if (snap.hasData) {
return snap.data;
} else {
//return CircularProgressIndicator();
return Container(height: 0.0, width: 0.0);
}
})
: Container(
height: 0.0,
width: 0.0,
),
Related
screenshot image for showMaterialScrollPicker
Hey guys , I'm new flutter learner so can I change the color of unselected items in showMaterialScrollPicker ?
Hey guys , I'm new flutter learner so can I change the color of unselected items in showMaterialScrollPicker ?
child: GestureDetector(
onTap: (){
showMaterialScrollPicker(
context: context,
items: GpaCubit.get(context).grades,
selectedItem: GpaCubit.get(context).gradeValues[index],
title: 'Pick your grade' ,
headerTextColor: Colors.white,
backgroundColor: calculatorColor ,
// backgroundColor: Colors.teal,
headerColor: Colors.cyan.withOpacity(0.7),
buttonTextColor: Colors.white,
showDivider: false,
onChanged: (value){
GpaCubit.get(context).changeGradeValue(value, index) ;
},
);
},
child:
GpaCubit.get(context).gradeValues[index] == 'null' ?
Center(
child: Text(
'Grade' ,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
) :
Center(
child: Text(
GpaCubit.get(context).gradeValues[index] ,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.cyan,
fontSize: 22,
fontWeight: FontWeight.w500,
),
),
) ,
),
You can use CupertinoPicker , both provide the similar UI, the showMaterialScrollPicker using Stack widget.
Future<dynamic> customPicker(BuildContext context) {
return showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) => AlertDialog(
title: Text("Select Grade"),
content: CupertinoPicker(
itemExtent: 60,
onSelectedItemChanged: (value) {
selected = grades[value];
setState(() {});
},
children: grades
.map(
(e) => Center(
child: Text(
"$e",
style: TextStyle(
color: selected == e
? Colors.green
: Colors.cyanAccent),
),
),
)
.toList(),
),
),
);
},
);
}
what'd I miss ? is it because dart version or other things ,
environment:
sdk: ">=2.16.1 <3.0.0"
1. error
Error: The value 'null' can't be returned from a function with return
type 'Widget' because 'Widget' is not nullable.
'Widget' is from 'package:flutter/src/widgets/framework.dart'
// Alertdialog box showing
showAlertDialog(BuildContext context) {
AlertDialog alert = AlertDialog(
content: Padding(
padding: const EdgeInsets.only(top: 22.0, bottom: 22),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
child: Text(
"Would you like to get latest updates and notifications?",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.black, fontSize: 18),
),
),
SizedBox(height: 24),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
FlatButton(
onPressed: () {
setState(() {
snackBarText = "You will not recive notifications.";
});
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => null));
},
child: Text(
"DENY",
style:
TextStyle(color: Colors.white, fontSize: 16, height: 1.2),
),
color: Colors.purple,
),
SizedBox(width: 10),
FlatButton(
onPressed: () {
setState(() {
snackBarText = "You will recive notifications.";
});
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => null));
},
child: Text(
"ALLOW",
style: TextStyle(
color: Colors.purple, fontSize: 16, height: 1.2),
),
shape: RoundedRectangleBorder(
side: BorderSide(width: 1, color: Colors.purple)),
color: Colors.white,
)
])
],
),
),
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
**2.**error
lib/ua_Screens.dart:29:8: Error: Field '_image' should be initialized
because its type 'File' doesn't allow null.
'File' is from 'dart:io'. File _image;
^^^^^^
File _image;
Future get_image() async {
final image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = image;
});
}
I think you write wrong code, try below code hope its help to used. and one thing dont used FlatButton used TextButton because FlatButton is depriciated by flutter.
Refer TextButton
Your Alert Dialog function:
showAlertDialog(BuildContext context) {
return AlertDialog(
content: Padding(
padding: const EdgeInsets.only(top: 22.0, bottom: 22),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
child: Text(
"Would you like to get latest updates and notifications?",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.black, fontSize: 18),
),
),
SizedBox(height: 24),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
FlatButton(
onPressed: () {
setState(() {});
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => null));
},
child: Text(
"DENY",
style:
TextStyle(color: Colors.white, fontSize: 16, height: 1.2),
),
color: Colors.purple,
),
SizedBox(width: 10),
FlatButton(
onPressed: () {
setState(() {});
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => null));
},
child: Text(
"ALLOW",
style: TextStyle(
color: Colors.purple, fontSize: 16, height: 1.2),
),
shape: RoundedRectangleBorder(
side: BorderSide(width: 1, color: Colors.purple)),
color: Colors.white,
)
])
],
),
),
);
}
Your Widget:
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return showAlertDialog(context);
},
);
},
child: Text('Pressed Me'),
),
Your result screen->
Your alert dialog->
Your Second error refer my answer here and here
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());
},
),
Flutter & AlertDialog : My app doesn't show the alert dialog after loading.
Even the 2 prints before and after the alert dialog was printed, the dialog was skip. Why is that? Please help me with this.
onTap: () async {
if (_formKey.currentState.validate() &&
_ratingStar > 0) {
setState(() {
_loading = true;
});
dynamic result =
await User_DatabaseService().uploadFeedback(
comment: review );
setState(() {
_loading = false;
});
if (result) {
print('Before dialog');
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(6.0))),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(
vertical: 60, horizontal: 10),
child: Text(
//'Please rate with star',
'평가해 주셔서 감사합니다!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
height: 50,
//color: primaryColor,
child: Text(
AppLocalizations.of(context)
.translate('OKAY'),
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold),
),
),
),
],
),
);
},
);
print('After dialog');
Navigator.pop(context);
} else {
print('Sth wrong');
}
} else {
print('Not submit');
}
},
Please have a look on my code and tell me what's wrong. Thank you. I am looking forward to hearing from you.
Here is the problem:
if (result) {
print('Before dialog');
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(6.0))),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(
vertical: 60, horizontal: 10),
child: Text(
//'Please rate with star',
'평가해 주셔서 감사합니다!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
height: 50,
//color: primaryColor,
child: Text(
AppLocalizations.of(context)
.translate('OKAY'),
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold),
),
),
),
],
),
);
},
);
print('After dialog');
Navigator.pop(context);
} else {
print('Sth wrong');
}
You are presenting the dialogue, then popping it. Make sure to add the Navigator.pop(context) method, only after you click a button on the alert dialogue. So, rewrite the code like this:
if (result) {
print('Before dialog');
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(6.0))),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(
vertical: 60, horizontal: 10),
child: Text(
//'Please rate with star',
'평가해 주셔서 감사합니다!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
height: 50,
//color: primaryColor,
child: Text(
AppLocalizations.of(context)
.translate('OKAY'),
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold),
),
),
),
],
),
);
},
);
print('After dialog');
} else {
print('Sth wrong');
}
The issue lies in the line just after the print('After dialog') line. You are doing a Navigator.pop(context); which is basically removing the dialog from the navigation stack.
In flutter:
The showDialog() is used to show the dialog. And the Navigator.pop(context) is used to remove the dialog
When I click the button, I want to show a dialog when the process is finished, but although I add the dialog part I cant see alertdialog in my screen.
Where am I doing wrong? here's my code;
child: InkWell(
onTap: () async {
final selectedLanguage =
await SharedPreferences.getInstance();
final setLanguage =
selectedLanguage.getString('selectedLanguage');
String language =
allTranslations.getLocaleKey(setLanguage);
_forgotPassword =
await createPasswordCode(_controller.text, language);
_dialog('Try');
},
child: Center(
child: Text(
'Send',
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
And here's my dialog part
Widget _dialog(String title) {
return AlertDialog(
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
actions: [
FlatButton(
child: Text('Approve'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
}
You need to call the alertDialog inside showDialog.
InkWell(
onTap: () async {
final selectedLanguage =
await SharedPreferences.getInstance();
final setLanguage =
selectedLanguage.getString('selectedLanguage');
String language =
allTranslations.getLocaleKey(setLanguage);
_forgotPassword =
await createPasswordCode(_controller.text, language);
showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return _dialog("try");
},
);
},
child: Center(
child: Text(
'Send',
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
))
Widget _dialog(String title) {
return showDialog(
barrierDismissible: false,
context: context,
child: AlertDialog(
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
actions: [
FlatButton(
child: Text('Approve'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
)
),
);
}
you need the showDialog method to show the dialog box