Home > Enterprise >  Page orientation, (navigator)
Page orientation, (navigator)

Time:08-26

I want it to return to the home page after the post is shared in my app. It will go back to home page after it succes. How can I do it? My codes are as follows.

void postImage(
        String uid,
        String username,
        String profImage,
    
      ) async {
        setState(() {
          _isLoading = true;
        });
        try {
          String res = await FirestoreMethods().uploadPost(
              _descriptionController.text, _file, uid, username, profImage,);
    
          if (res == 'succes') {
            setState(() {
              _isLoading = false;
            });
            showSnackBar('Posted!', context);
            clearImage();
          } else {
            showSnackBar(res, context);
          }
        } catch (e) {
          showSnackBar(e.toString(), context);
        }
      }

Button

TextButton(
              onPressed: () => postImage(
                    user.uid,
                    user.username,
                    user.photoUrl,
                ),
              child: const Text(
                'Paylaş',
                style: TextStyle(
                  color: Colors.blueAccent,
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ))
        ],

CodePudding user response:

Future<void> postImage( // edit this line
        String uid,
        String username,
        String profImage,
    
      ) async {
        setState(() {
          _isLoading = true;
        });
        try {
          String res = await FirestoreMethods().uploadPost(
              _descriptionController.text, _file, uid, username, profImage,);
    
          if (res == 'succes') {
            setState(() {
              _isLoading = false;
            });
            showSnackBar('Posted!', context);
            clearImage();
          } else {
            showSnackBar(res, context);
          }
        } catch (e) {
          showSnackBar(e.toString(), context);
        }
      }

TextButton(
              onPressed: () async{ // edit this line
await postImage(. // edit this line
                    user.uid,
                    user.username,
                    user.photoUrl,
                )


      Navigator.of(context).push(MaterialPageRoute(builder: ((context) => HomePage())); // add this line

},
              child: const Text(
                'Paylaş',
                style: TextStyle(
                  color: Colors.blueAccent,
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              )
)
  • Related