Home > Back-end >  Flutter) Dialog TextButton's function is executed automatically without tab the button
Flutter) Dialog TextButton's function is executed automatically without tab the button

Time:01-11

Hi I'm newbie at coding and Flutter is my first language.

I want to delete Firestore data(Chat List) which is in the ListView from the StreamBuilder when user long tab the 'OK' button in AlertDialog.

(Process Summary:

  1. longPress one item of the ListViewBuilder
  2. show check AlertDialog
  3. tab 'OK'
  4. OK button's Function execute)
But when i longPress the item, then the deleteFunction(OK button's function) is automatically executed before i see the dialog.

How can I make it?

It is really difficult!

Here is my code

StreamBuilder(
              stream: chatRoomDB,
                builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (snapshot.connectionState == ConnectionState.active &&
                      snapshot.data != null) {
                    return ListView(
                        children: snapshot.data!.docs
                            .map<Widget>((DocumentSnapshot ds) {
                      Map<String, dynamic> data =
                          ds.data()! as Map<String, dynamic>;
                      receiverProfileImageURL = data['ProfileImageURL'];
                      receiverUID = data['UserID'];
                      receiverNickName = data['NickName'];
                      msgContent = data['MsgContent'];
                      chatID = data['ChatID'];
                      
                     return Container(
                        margin: EdgeInsets.all(3),
                        child:  ListTile(
                            shape: Border(
                                bottom:
                                    BorderSide(color: Colors.grey, width: 1)),
                            onTap: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => ChattingRoom(
                                            chatID: data['ChatID'],
                                            receiverUID: data['UserID'],
                                            receiverImageURL:
                                                data['ProfileImageURL'],
                                            receiverNickName: data['NickName'],
                                          )));
                            },
                            onLongPress: () => checkDialog2(
                                context: context,
                                msg: 'Delete?',
                                okButtonString: 'OK',
                                okFunction: chatDBDeleteFunction(
                                    data['UserID'], data['ChatID']),
                                noButtonString: 'No'),
                            leading: Container(
                                child: CircleAvatar(
                              foregroundImage: NetworkImage(
                                '$receiverProfileImageURL',
                              ),
                              backgroundImage:
                                  NetworkImage('$errorImageURL'),
                            )),
                            title: Text('$receiverNickName'),
                        ),
                      );
                    }).toList());
                  }
                  return Container();
                })
checkDialog2({
  required context,
  required String? msg,
  required String? okButtonString,
  required Future<dynamic> okFunction,
  required String? noButtonString,
}) {
  print(1);
  okFunctions(okFunc) {
    print(3);
    okFunction;
    Navigator.pop(context);
    print(4);
  }

  print(5);
  showDialog(
      context: context,
      builder: (context) {
        print(2);
        return AlertDialog(content: Text('$msg'), actions: <Widget>[
          TextButton(
            onPressed: () => okFunctions(okFunction),
            child: Text(
              '$okButtonString',
              textAlign: TextAlign.end,
            ),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text(
              '$noButtonString',
              textAlign: TextAlign.end,
            ),
          ),
        ]);
      });
}
  Future chatDBDeleteFunction(receiverUID, chatID) async {
    await FirebaseFirestore.instance
        .collection('ChatMessage')
        .doc('${FirebaseAuth.instance.currentUser?.uid}')
        .collection(chatID)
        .get()
        .then((QuerySnapshot qs) => {
              qs.docs.forEach((DocumentSnapshot snapshot) {
                snapshot.reference.delete();
              })
            });

Log Order is, print 1->5>2 and automatically deleted showDialog.

At that time(list item already disappeared), if i tab the 'OK' button, then print '3' and error(because there is nothing to delete)

(ps. although i change the okfunction to the void Function, it starts automatically. same)

Thanks for your help

CodePudding user response:

I would prefer passing the UserId and ChatId and then call the chatDBDeleteFunction(receiverUID, chatID) Function and pass the values from values

 TextButton(
        onPressed: () {
         Class.chatDBDeleteFunction(receiverUID, chatID)


           }
    )

The chatDBDeleteFunction(receiverUID, chatID) function can be static

  •  Tags:  
  • Related