FLUTTER button onpressed navigator push got errors

247 Views Asked by At

I've made a button to send data to firebase. I want when the button is pressed, the button is not only to send data to firebase, but I want the button makes users can go back to the homepage.

I used setData for sending the data, and I used Navigator.push, but the navigator push is errors.

final sdButton = Material( 
  elevation: 5,
  borderRadius: BorderRadius.circular(5),  
  color: const Color(0xFFD3DEDC),  
  child: MaterialButton(   
    padding: const EdgeInsets.fromLTRB(20,15,20,15),
    minWidth: MediaQuery.of(context).size.width, 
    onPressed: setData {
      Navigator.push(context, 
      MaterialPageRoute(   
        builder:(context) => Homepage(),));
  
    },

and this is the code for setData

  setData(){
    dref.child(user!.uid).set({
      'user_id' : user!.uid,
      'intime' : '15.17'
    }
    );
  }
  
2

There are 2 best solutions below

0
On
 onPressed: () {
      setData();
      Navigator.push(context, MaterialPageRoute(   
        builder:(context) => Homepage(),));
    },
0
On

Please update onPress method to like as below.

 onPressed: (){
          setData();
          Navigator.push(context, 
          MaterialPageRoute(   
            builder:(context) => Homepage(),));
    },