flutter 返回类型“Future< Null>”不是闭包上下文所要求的“Widget

mi7gmzs6  于 2023-01-02  发布在  Flutter
关注(0)|答案(2)|浏览(223)

Help返回类型'Future'不是'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);
  }

代码结构

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);
    })));

需要帮助谢谢。
错误"The return type 'Future<Null>' isn't a 'Widget', as required by the closure's context."

h79rfbju

h79rfbju1#

更改Future函数以包含Map<String, dynamic>return type

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);
  }

builder中使用FutureBuilder,如下所示:

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();                                  
                                 }
         
           )));
});
lsmd5eda

lsmd5eda2#

您必须在onTap中使用您的未来Function,然后使用**showSnackbar**。

示例。

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);
        })));
});

相关问题