skip to Main Content

Help The return type ‘Future’ isn’t a ‘Widget’,

Future deleteProduct(String productId) async {
    String url = 'http://127.0.0.1:8000/api/products' + productId;
    var response = await http.delete(Uri.parse(url));
    return json.decode(response.body);
  }

Code structure

GestureDetector(
    onTap: () {
        final snackBar = SnackBar(
        content: const Text('Data berhasil Dihapus'));
        Navigator.push( context,
        MaterialPageRoute(
            builder: (context) =>deleteProduct((snapshot.dataas dynamic)['data'][index]['id'].toString()).then((value) {setState(() {});
      ScaffoldMessenger.of(context) .showSnackBar( snackBar);
    })));

Need Help Thanks.

Error "The return type 'Future<Null>' isn't a 'Widget', as required by the closure's context."

2

Answers


  1. You have to use your future Function in onTap and then use showSnackbar.

    Example.

        GestureDetector(
           onTap: () async {
             await deleteProduct((snapshot.dataas dynamic)['data'][index]['id'].toString()).then((){
             final snackBar = SnackBar(
                             content: const Text('Data berhasil Dihapus'));
           Navigator.push( context,
            MaterialPageRoute(
            builder: (context) =>deleteProduct((snapshot.dataas dynamic)['data'][index]['id'].toString()).then((value) {setState(() {});
              ScaffoldMessenger.of(context) .showSnackBar( snackBar);
            })));
    });
        
    
    Login or Signup to reply.
  2. Change your Future function to include return type of Map<String, dynamic>

    Future<Map<String,dynamic> deleteProduct(String productId) async {
        String url = 'http://127.0.0.1:8000/api/products' + productId;
        var response = await http.delete(Uri.parse(url));
        return json.decode(response.body);
      }
    

    And use FutureBuilder in the builder like:

        GestureDetector(
           onTap: () async {
             Navigator.push( context,
             MaterialPageRoute(
               builder: (context) => FutureBuilder(
                                       future: deleteProduct((snapshot.dataas dynamic)['data'][index]['id'].toString()),
                                       builder: (ctx, snapshot) {
                                         if (snapshot.connectionState == ConnectionState.done) {
                                           if (snapshot.hasError) {
                                             return Center(
                                                child: Text('${snapshot.error} occurred',
                                               ),
                                             );
                                           }
                                         } else if (snapshot.hasData) {
                                            final data = snapshot.data as String;
                                            return Center(
                                               child: Text('$data'),
                                            );
                                         }   
    
                                       return CircularProgressIndicator();                                  
                                     }
             
               )));
    });
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search