从 flutter 中的 jsonencode 获取错误状态代码 500

问题描述 投票:0回答:1
 onPressed: () async {
          final url = Uri.parse('https://anash-visual-qa.hf.space/run/predict');
          final response = await http.post(
            url,
            headers: {'Content-Type': 'application/json'},
            body: jsonEncode({
              'data': [
                'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==',
                'hello world',
              ],
            }),
          );

          if (response.statusCode == 200) {
            final data = jsonDecode(response.body)['data'];
            print(data);
            print('Request Success with status: ${response.statusCode}.');
            // Do something with data
          } else {
            print('Request failed with status: ${response.statusCode}.');
          }

          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => output(
                      response.body,
                    )),
          );

当数据以上面给出的字符串格式在 json 编码中传递时,我得到了正确的输出。

............................................... ..................................................... ..................................................... ...............................

onPressed: () async {
                // List<int> _bytes = await imageFile!.readAsBytes();
                Uint8List imageBytes = await imageFile!.readAsBytes();
                img.Image image = img.decodeImage(imageBytes)!;
                img.Image pngImage = img.copyResize(image, width: 800);
                Uint8List pngBytes = img.encodePng(pngImage);
                String base64String = base64Encode(pngBytes);
                // String _base64String = base64.encode(_bytes);
                String textInput = fieldText.text;
                if (imageFile != null &&
                    base64String.isNotEmpty &&
                    textInput.isNotEmpty) {
                  final url =
                      Uri.parse('https://anash-visual-qa.hf.space/run/predict');
                  final response = await http.post(
                    url,
                    headers: {'Content-Type': 'application/json'},
                    body: jsonEncode({
                      'data': [base64String, textInput],
                    }),
                  );

                  if (response.statusCode == 200) {
                    final data = jsonDecode(response.body)['data'];
                    print(data);
                    print(
                        'Request Success with status: ${response.statusCode}.');
                    // Do something with data
                  } else {
                    print(
                        'Request failed with status: ${response.statusCode}.');
                  }

                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => output(
                              response.body,
                            )),
                  );
                } else {
                  print(
                      'Error: Image file, base64 string, or text input is missing.');
                }

但是当我尝试传递用户输入的数据时,我收到错误状态代码 500

任何人都可以帮助解决问题吗

flutter api dart http-status-code-500 jsonencode
1个回答
0
投票
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as path;

extension Base64Extension on File {
  Future<String> toBase64() async {
    final photoInBytes = await this.readAsBytes();

    return "data:image/${path.extension(this.path)
        .replaceFirst(RegExp(r'.'), "")}"
        ";base64,${base64Encode(photoInBytes)}";
  }
}

在你的图像上尝试这个扩展

File
你可以获得正确的 base64 字符串,你的问题就会解决。

© www.soinside.com 2019 - 2024. All rights reserved.