I have a problem when I try to delete the item and it doesn't accept to delete the item. I am using ConfirmDismiss.
Error:
Unhandled Exception: type 'Null' is not a subtype of type 'bool'
_DisMissState.build..
(package:advanced_part_2/dismissible.dart:84:30)
_DismissibleState._handleDismissStatusChanged
(package:flutter/src/widgets/dismissible.dart:498:11)
Code:
confirmDismiss: (DismissDirection dir) async {
if (dir == DismissDirection.startToEnd) {
// AlertDialog ad =
final bool res = await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Are You Sure you want to delete'),
actions:<Widget> [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
'cancel',
)),
ElevatedButton(
onPressed: () {
setState(() {
genList.removeAt(index);
});
Navigator.of(context).pop();
},
child: Text(
'Delete',
style: TextStyle(color: Colors.red),
))
],
);
},
);
return res;
} else {
return true;
}
},
It is because when your calling the following code:
final bool res = await showDialog(...);
it will return a null value, see https://api.flutter.dev/flutter/material/showDialog.html
And, you also not giving any return value when clicking the button:
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
'cancel',
)),
You can't use Navigator.of(context).pop();. Instead, you need to use either:
Navigator.pop(context, false);
or
Navigator.pop(context, true);
Then, after that, you need to handle the nullable returned value from show dialog by giving a default value when null. Something like this:
bool res = await showDialog(...);
if(res == null) res = false;
showDialog can return null when the dialog is dismissed (e.g. user clicks outside the dialog) so you've to account for that by changing the type to:
final bool? res = await showDialog(/* your code */);
then in your logic below, you have to check for null:
if(res == null) {
// handle dismiss
} else if (res == false) {
// handle cancel
} else {
// handle confirm/true
}
As #Er1 mentioned in the comment, you'll also need to pass true or false when popping the dialog:
Navigator.of(context).pop(true);
// or
Navigator.of(context).pop(false);
The above still applies since barrierDismissible is true by default.
final bool res = await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Are You Sure you want to delete'),
actions:<Widget> [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: Text(
'cancel',
)),
ElevatedButton(
onPressed: () {
setState(() {
genList.removeAt(index);
});
Navigator.of(context).pop(true);
},
child: Text(
'Delete',
style: TextStyle(color: Colors.red),
))
],
);
},
);
Add false and true to the pop() to make the showDialog return a boolean.
But keep in mind if you don't set barrierDismissible in showDialog to false you can get null returned if you tap outside of the dialog.
Related
How would return a value from a dialog without using a global variable or a state management library?
you can use then
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text('Wanna Exit?'),
actions: [
FlatButton(
onPressed: () => Navigator.pop(context, false), // passing false
child: Text('No'),
),
FlatButton(
onPressed: () => Navigator.pop(context, true), // passing true
child: Text('Yes'),
),
],
);
}).then((exit) {
if (exit == null) return;
if (exit) {
// user pressed Yes button
} else {
// user pressed No button
}
});
I try to build cusom confirmation dialog. I tried many different options, but none of them worked for me as it should
Now I have stopped at this one:
Future<bool?> showConfirmationDialog(BuildContext context, String action, {String title = 'Confirmation'}) {
bool result = false;
var confirmDialog = AlertDialog(
title: Text(title),
content: SizedBox(
height: 50,
child: Text(
action,
),
),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () {
Navigator.pop(context, true);
},
),
TextButton(
child: const Text("Cancel"),
onPressed: () {
Navigator.of(context).pop(false);
},
)
],
);
return showDialog<bool>(
context: context,
builder: (BuildContext context) {
return confirmDialog;
},
);
}
but I always get null in result:
confirmDismiss: (DismissDirection direction) async {
var result = await showConfirmationDialog(context, 'Are you sure?');
// here result is null always
if (result == true){
print('yes');
return true;
}
else if(result == false){
print('no');
return false;
}
}
How to make a confirmation dialog box in flutter correctly?
I am using the flutter_reactive_ble_example to connect to my Bluetooth module by modifying the file device_list.dart.
and I wonder how do I re prompt the user if password is wrong.
I'm fairly new to flutter, please do ask more details if required.
here is the code snippet that I currently have:
Flexible(
child: ListView(
children: widget.scannerState.discoveredDevices
.map(
(device) => ListTile(
title: Text(tile.name),
subtitle: Text("${tile.name}\n: ${tile.sub}"),
leading: const ConnectIcon(),
onTap: () async {
//stop the scan
widget.stopScan();
//connect to the device
await widget.deviceConn.connect(device.id);
//prompt user for password
final inputData = await showDialog(
context: context,
barrierDismissible:
false, // prevent user from closing the dialog by pressing outside the dialog
builder: (_) {
String userData = "";
return AlertDialog(
title: new Text("Enter Password"),
content: new TextField(
onChanged: (value) {
userData = value;
},
),
actions: <Widget>[
ElevatedButton(
child: Text('Ok'),
onPressed: () async {
//on press subscribe and send the password
response = await ble.subscribeToCharacteristic(characteristic);
//if data failure check, how do I reshow this showDialog??
response.listen((event) {
if(event == 1){
//if return 1, password correct
Navigator.of(context).pop(userData);
}else{
//if not reshow Dialog
//howw?
}
}
//send password
ble.writeCharacteristicWithoutResponse(characteristic, value: userData);
},
)
],
);
},
);
Navigator.of(context).pop(
inputData); // pass data back to the previous page
},
),
)
.toList(),
),
),
You can use a recursion I think, here an example
Future _showPasswordDialog(){
return showDialog(
context: context,
barrierDismissible:
false, // prevent user from closing the dialog by pressing outside the dialog
builder: (_) {
String userData = "";
return AlertDialog(
title: new Text("Enter Password"),
content: new TextField(
onChanged: (value) {
userData = value;
},
),
actions: <Widget>[
ElevatedButton(
child: Text('Ok'),
onPressed: () async {
//on press subscribe and send the password
response = await ble.subscribeToCharacteristic(characteristic);
//if data failure check, how do I reshow this showDialog??
response.listen((event) {
if(event == 1){
//if return 1, password correct
Navigator.of(context).pop(userData);
}else{
//if not reshow Dialog
//howw?
Navigator.of(context).pop();
_showPasswordDialog();
}
}
//send password
ble.writeCharacteristicWithoutResponse(characteristic, value: userData);
},
)
],
);
},
);
}
separate the the alert prompting as another function, and return user details if login success else return null.
Future<String> promptAlert(BuildContext context){
return showDialog(
context: context,
barrierDismissible:
false, // prevent user from closing the dialog by pressing outside the dialog
builder: (_) {
String userData = "";
return AlertDialog(
title: new Text("Enter Password"),
content: new TextField(
onChanged: (value) {
userData = value;
},
),
actions: <Widget>[
ElevatedButton(
child: Text('Ok'),
onPressed: () async {
//on press subscribe and send the password
response = await ble.subscribeToCharacteristic(characteristic);
//if data failure check, how do I reshow this showDialog??
response.listen((event) {
if(event == 1){
//if return 1, password correct
Navigator.of(context).pop(userData);
}else{
Navigator.of(context).pop();
}
}
//send password
ble.writeCharacteristicWithoutResponse(characteristic, value: userData);
},
)
],
);
},
);
}
and check for the returned value is not null on the ListItem onTap
bool isLogin = (await promptAlert(context)) !=null;
while(isLogin ){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 2),
content: Text('Login Failed Try again')));
String user= await Future.delayed(
Duration(seconds: 2), () => promptAlert(context));
isLogin = user !=null;
}
If you want to show a snackbar and delayed alert,
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 2),
content: Text('Login Failed Try again'),
));
Future.delayed(
Duration(seconds: 2), () => promptAlert(context));
I have problem in button to search user by phone number. When I press button for the first time, query can't retrieve the data, like this : Debug Console 1. But if I press button again for the second time or more, query can retrieve the data, like this : Debug Console 2.
This is my button code :
Widget tmblKonfirm() {
return Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
child: FlatButton(
onPressed: () async{
await pr.show();
print('NoHP : ' + nohp);
final dataUser = FirebaseDatabase.instance
.reference()
.child("users")
.orderByChild("pengguna_nomor")
.equalTo(nohp);
print('Data User : ' + dataUser.toString());
dataUser.once().then((DataSnapshot snapshot) {
Map<dynamic, dynamic> data = snapshot.value;
setState(() {
testUser = snapshot.value.toString();
});
data.forEach((key, values) {
lists.add(values);
});
});
print('User 1 : ' + lists.toString());
pr.hide();
if (_formKeyKonfirm.currentState.validate()) {
print('User 2 : ' + testUser.toString());
if (testUser != 'null'){
// setState(() {
sttsUser = true;
// });
// showWidgetTopup();
}
else {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Text(
'Nomor HP tidak terdaftar'),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('OK'),
)
],
);
},
);
}
}
},
child: Text('Konfirmasi',
style: TextStyle(color: Colors.black, fontSize: 25)),
color: Colors.lightBlue,
),
));
}
dataUser is a Query so you always have to fetch the data snapshot (once() or onValue)
Instead of dataUser.once().then use await dataUser.once(), otherwise the snapshot callback can be actually called after the onPressed function finishes
In flutter, I have a showDialog() with cancel and confirm button. The confirm button will trigger a call to an API. What I need is to show a "loading..." in the showDialog window after the click and once the API call is finished to show a success or failure. How can I manage this? Or should I close the window, waiting for the reply and popup a new dialog window with success or false? Thx for any help or better suggestion. Here what I have so far:
void _showAlert(String textLabel, String action, String linkId) {
showDialog(
context: context,
//barrierDismissible: false, // on external click
builder: (_) => new AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
title: new Text(
'Please confirm:',
style: TextStyle(color: Colors.deepOrange, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
content: new Text(
textLabel,
style: new TextStyle(fontSize: 20.0),
),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: new Text('CANCEL')),
new FlatButton(
onPressed: () {
_postAction(action, linkId).then((_) => setState(() {}));
Navigator.pop(context);
},
child: new Text('I CONFIRM')),
],
));
}
You can try this,this is just the idea..
class _SampleState extends State<Sample> {
bool isSuccessFromApi = false;
bool isLoading = false;
Widget popup() {
showDialog(context: context, builder: (builder) {
return AlertDialog(
content: !isSuccessFromApi ? Container(
child: Text('Are you Sure???'),
) : Container( child: isLoading ? CircularProgressIndicator() : Text('Success'),),
actions: <Widget>[
Text('Cancel'),
InkWell(child: Text('OK'), onTap: apiCall,)
],
);
});
}
void apiCall(){
setState(() {
isLoading = true;
});
//call the api
//after success or failure
setState(() {
isLoading = false;
isSuccessFromApi = true;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: popup(),
);
}
}