C# 如何从.NET API接收图像文件并在Dart FLATTER应用程序中显示?
在我的Dart Flatter应用程序中,我从.NET Core API收到一个以字节字符串形式存在的图像文件。然后我将字符串编码到一个列表中。我将列表传递给Image.memory,但图像不会出现,也不会引发异常 这是用于在Dart Flatter应用程序中显示从.NET Core API加载的图像。我已经试过utf8.encode、String.runes和String.codeUnits utf-16。但是当我将列表传递到Image.memory时,没有一个可以工作 使用fromJson方法对从.NET API接收的JSON中的图像字节字符串进行编码的Dart颤振模型类:C# 如何从.NET API接收图像文件并在Dart FLATTER应用程序中显示?,c#,flutter,C#,Flutter,在我的Dart Flatter应用程序中,我从.NET Core API收到一个以字节字符串形式存在的图像文件。然后我将字符串编码到一个列表中。我将列表传递给Image.memory,但图像不会出现,也不会引发异常 这是用于在Dart Flatter应用程序中显示从.NET Core API加载的图像。我已经试过utf8.encode、String.runes和String.codeUnits utf-16。但是当我将列表传递到Image.memory时,没有一个可以工作 使用fromJson方
class SelfMonitorPhotoModel {
String id;
String name;
List<int> bytes;
SelfMonitorPhotoMetaData metaData;
SelfMonitorPhotoModel({
this.id,
this.name,
this.bytes,
this.metaData
});
factory SelfMonitorPhotoModel.fromJson(Map<String, dynamic> json) {
var bytesString = json["bytes"] as String;
List<int> bytesList = utf8.encode(bytesString);
print(bytesList);
return SelfMonitorPhotoModel(
id: json["id"],
bytes: bytesList,
metaData: SelfMonitorPhotoMetaData.fromJson(json["metaData"]),
name: json["name"]
);
}
Map<String, dynamic> toJson() => {
"Id": id,
"Name": name,
"MetaData": metaData,
"bytes": bytes
};
}
图像文件字节在C.NET API代码中为byte[]类型,在JSON中以字符串形式写入:
// Controller action returning the photo/ image model.
[HttpGet("Photos/TaskId/{taskId}")]
public async Task<IActionResult> GetPhotosByTaskId(string taskId)
{
try
{
return Ok(await _selfMonitorService.GetPhotosByTaskId(taskId));
}
catch (Exception e)
{
_logger.Error($"{e.Message} | {e.StackTrace}");
return BadRequest();
}
}
// The model class returned from the API containing the image file bytes.
public class SelfMonitorPhotoModel
{
public string Id { get; set; }
public string Name { get; set; }
public byte[] Bytes { get; set; }
public SelfMonitorPhotoMetaData MetaData { get; set; }
}
我希望图像会显示在Dart Flatter应用程序中。但是它不会显示,也不会抛出异常。根据,将在json中对base64中的字节数组进行编码
尝试替换:
var bytesString = json["bytes"] as String;
List<int> bytesList = utf8.encode(bytesString);
与
以原始形式提供http响应,以便我们能够理解它是否与输出等有关。
var bytesString = json["bytes"] as String;
List<int> bytesList = utf8.encode(bytesString);
var bytesString = json['bytes'] as String;
List<int> bytesList = base64.decode(bytesString);