提问人:Sahal Saifuddin 提问时间:5/22/2023 最后编辑:Sahal Saifuddin 更新时间:5/22/2023 访问量:31
如何从 RestApi flutter 获取文件
how to fetch files from RestApi flutter
问:
我正在尝试从 Rest Api 获取图像文件以在 CircleAvatar 中显示它 这是 Api 的照片
我为此使用了 Future:
Future<Int8List> getProfileImg() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String? accessToken = prefs.getString('accessToken');
print(accessToken);
final response = await http.get(
Uri.parse(RestClient.getProfileImage),
headers: {
'Content-Type': 'application/json',
'Authorization': ' Bearer $accessToken',
},
);
if (response.statusCode == 200) {
final ByteData data =
await NetworkAssetBundle(Uri.parse(response.body)).load(response.body);
final Int8List bytes = data.buffer.asInt8List();
return bytes;
// return User.fromJson(json.decode(response.body.toString()));
} else {
print(response.statusCode);
throw Exception('Failed to get profile image');
}
}
然后,当我想打印print(response.bodyBytes)时,它显示如下:
[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 5, 0, 0, 0, 5, 0, 8, 2, 0, 0, 0, 151, 134, 104, 160, 0, 0, 140, 176, 73, 68, 65, 84, 120, 218, 236, 221, 137, 82, 91, 215, 158, 232, 225, 251, 254, 207, 17, 16, 2, 73, 123, 107, 107, 96, 30, 13, 216, 198, 54, 96, 131, 153, 12, 24, 11, 52, 236, 17, 231, 13, 110, 9, 146, 147, 56, 113, 218, 118, 226, 1, 201, 95, 213, 87, 167, 250, 118, 165, 115, 186, 221, 105, 237, 245, 187, 107, 173, 255, 250, 127, 55, 189, 62, 0, 0, 0, 140, 189, 255, 231, 143, 0, 0, 0, 0, 1, 12, 0, 0, 0, 2, 24, 0, 0, 0, 4, 48, 0, 0, 0, 8, 96, 0, 0, 0, 16, 192, 0, 0, 0, 32, 128, 1, 0, 0, 64, 0, 3, 0, 0, 128, 0, 6, 0, 0, 64, 0, 3, 0, 0, 128, 0, 6, 0, 0, 0, 1, 12, 0, 0, 0, 2, 24, 0, 0, 0, 4, 48, 0, 0, 0, 8, 96, 0, 0, 0, 16, 192, 0, 0, 0, 32, 128, 1, 0, 0, 64, 0, 3, 0, 0, 32, 128, 1, 0, 0, 64, 0, 3, 0, 0, 128, 0, 6, 0, 0, 0, 1, 12, 0, 0, 0, 2, 24, 0, 0, 0, 4, 48, 0, 0, 0, 8, 96, 0, 0, 0, 16, 192, 0, 0, 0, 32, 128, 1, 0, 0, 16, 192, 0, 0, 0, 32, 128, 1, 0, 0, 64, 0, 3, 0, 0, 128,
之后,我想在 CircleAvatar 中将其用作:
Widget buildProfileImage() => CircleAvatar(
radius: profileHeight / 2,
backgroundColor: Colors.grey.shade800,
child: Image.memory(_user),
// backgroundImage: NetworkImage(
// _user.asStream(),
// 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png',
// ),
);
但这里的问题是我不能使用 Image.memory 或 byteStream。
答:
0赞
MrMoon
5/22/2023
#1
在后台使用它图像
CircleAvatar(
backgroundImage: MemoryImage(decoded),
),
如果有效,请向上箭头,
评论
0赞
Sahal Saifuddin
5/23/2023
这是我试图在代码中做的事情,但它没有用
0赞
MrMoon
5/23/2023
尝试相同的内容,你的方式有点不同,我在工作中这样做,这是工作
评论