Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理来自API的消息-颤振_Api_Flutter_Dart - Fatal编程技术网

如何处理来自API的消息-颤振

如何处理来自API的消息-颤振,api,flutter,dart,Api,Flutter,Dart,我是颤振方面的新手,我在处理API请求。来自服务器的API响应如下: { "code": 1, "message": "User name updated with success !", "data": [], "error": [], "status": 200 } 我想在snackbar中向用户显示消息内容 我的代码: Fut

我是颤振方面的新手,我在处理API请求。来自服务器的API响应如下:

{
    "code": 1,
    "message": "User name updated with success !",
    "data": [],
    "error": [],
    "status": 200
}
我想在snackbar中向用户显示消息内容

我的代码:

  Future<String> editUserProfile(
      String name, String email, String adress) async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    String token = localStorage.getString('access_token');
    await checkInternet();
    Map<String, String> headers = {
      'Content-type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token'
    };
    Map<String, dynamic> body = {
      'name': name,
      'email': email,
      'adress': adress,
    };
  
    var response = await http.post(Uri.parse(ApiUtil.MODIFY_USER_PROFILE),
        headers: headers, body: jsonEncode(body));
    print(response.statusCode);
    print(response);
    inspect(response);

    if (response.statusCode == 200) {
      if (response.body.isNotEmpty) {
        body = jsonDecode(response.body);
      }
      var data = body['message'];
      return data;
    } else {
      throw Exception('Failed to modify name');
    }
  }


 void editUserProfile() async {
    setState(() {});

    String name = _nameController.text;
    String email = _emailController.text;
    String adress = _adressController.text;

    userApi.editUserProfile(name, email, adress).then((data) {
      if (data != null) {
        Navigator.pop(context);

     
      }
   
    }).catchError((error) {
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text(error.toString())));
    });
    setState(() {});
  }

由于您已经在将来正确返回了数据,所以只需使用它来显示一个SnackBar

if (data != null) {
  Navigator.pop(context);

  // Add this line
  ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data)));
}

你似乎把一切都准备好了。在我的案例中,似乎出现了什么问题?我无法获得消息:用户名更新成功!当您检查
数据时,
数据的值是多少空的
?我添加了一个答案,你是这样尝试的吗?太好了。。。非常感谢,但我还有其他问题。从api获取数据后,我希望保持在同一屏幕上。我认为这是setState((){})的问题。如何重新解决它?删除
Navigator.pop(context);
行。它的作用是,它删除当前页面,因此您可以返回上一页是的,您可以添加
setState((){enableadress=false;})
在新的
快捷键上
我给你的代码在单个
设置状态中,逐行添加所有代码
if (data != null) {
  Navigator.pop(context);

  // Add this line
  ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data)));
}