如何从 FLUTTER 中的 HTTP 方法获取特定值?

问题描述 投票:0回答:2

我的输出是这样的:

{
"scope": [],
"_id": "62413827f85e740dd8af749d",
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJpc3N1ZXIiLCJzdWIiOnsidXNlcklkIjoiNjI0MTM4MjdmODVlNzQwZGQ4YWY3NDlhIn0sImlhdCI6MTY0ODQ0MTM4M30.cNE32yojMlbohsOtgB2docCsZk8UPqEbPVTizV--rMs",
"user": {
    "_id": "62413827f85e740dd8af749a",
    "email": "[email protected]",
    "password": "hello",
    "phone": "01723456789",
    "createdAt": "2022-03-28T04:23:03.334Z",
    "updatedAt": "2022-03-28T04:23:03.334Z",
    "__v": 0
},
"createdAt": "2022-03-28T04:23:03.348Z",
"updatedAt": "2022-03-28T04:23:03.348Z",
"__v": 0
}

如何在flutter中从“user”获取“email”的值?我正在使用以下代码。

http
      .post(Uri.parse(url),
          headers: {"Content-type": "application/json;charset=UTF-8"},
          body: jsonEncode(model))
      .then((value) {
    var access = jsonDecode(value.body);
flutter dart http-post http-get
2个回答
0
投票

首先你应该使用 model 。不使用模型它会对你有帮助

这只是一个如何从地图获取值的示例

var apiResp = jsonDecode('''
    {
    "scope": [],
    "_id": "62413827f85e740dd8af749d",
    "access_token":
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJpc3N1ZXIiLCJzdWIiOnsidXNlcklkIjoiNjI0MTM4MjdmODVlNzQwZGQ4YWY3NDlhIn0sImlhdCI6MTY0ODQ0MTM4M30.cNE32yojMlbohsOtgB2docCsZk8UPqEbPVTizV--rMs",
    "user": {
      "_id": "62413827f85e740dd8af749a",
      "email": "[email protected]",
      "password": "hello",
      "phone": "01723456789",
      "createdAt": "2022-03-28T04:23:03.334Z",
      "updatedAt": "2022-03-28T04:23:03.334Z",
      "__v": 0
    },
    "createdAt": "2022-03-28T04:23:03.348Z",
    "updatedAt": "2022-03-28T04:23:03.348Z",
    "__v": 0
  }
  ''');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('${apiResp['user']['email']}')),

这里 jsonDecode 接受字符串值,所以我这样做了

'''
    {
    "scope": [],
    "_id": "62413827f85e740dd8af749d",
    "access_token":
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJpc3N1ZXIiLCJzdWIiOnsidXNlcklkIjoiNjI0MTM4MjdmODVlNzQwZGQ4YWY3NDlhIn0sImlhdCI6MTY0ODQ0MTM4M30.cNE32yojMlbohsOtgB2docCsZk8UPqEbPVTizV--rMs",
    "user": {
      "_id": "62413827f85e740dd8af749a",
      "email": "[email protected]",
      "password": "hello",
      "phone": "01723456789",
      "createdAt": "2022-03-28T04:23:03.334Z",
      "updatedAt": "2022-03-28T04:23:03.334Z",
      "__v": 0
    },
    "createdAt": "2022-03-28T04:23:03.348Z",
    "updatedAt": "2022-03-28T04:23:03.348Z",
    "__v": 0
  }
  '''

0
投票
final TextEditingController _nameController = TextEditingController();

  Future<void> createItem(String name) async {
    try {
      final response = await http.post(
        Uri.parse('http://localhost:5000/items'),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
        body: json.encode({'name': name}),
      );

      if (response.statusCode == 201) {
        // Successfully created the item
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Item created successfully!')),
        );
        Navigator.pop(context); // Return to the previous screen
      } else {
        // Handle server error
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to create item.')),
        );
      }
    } catch (e) {
      // Handle network error
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: $e')),
      );
    }
  }


  ElevatedButton(
              onPressed: () {
                if (_nameController.text.isNotEmpty) {
                  createItem(_nameController.text);
                } else {
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text('Please enter a valid name')),
                  );
                }
              },
              child: Text('Create Item'),
            ),
© www.soinside.com 2019 - 2024. All rights reserved.