gpt4 book ai didi

flutter - Dart/flutter : How can I display Images from API on localhost in emulator

转载 作者:行者123 更新时间:2023-12-05 07:09:27 27 4
gpt4 key购买 nike

当我想在本地主机上显示来 self 的 API 的图像模拟器时遇到问题它出现的所有文本,当我通过它显示的图像 url 模拟器浏览器时,但是当我在应用程序小部件 (NetworkImage(imageUrl) 中显示图像时,我得到以下异常

图片资源服务捕获异常═════════════════════════════════════════ ═══════抛出以下 SocketException 解析图像编解码器:操作系统错误:连接被拒绝,errno = 111,地址 = 127.0.0.1,端口 = 48458

当抛出异常时,这是堆栈:

0 NetworkImage._loadAsync(包:flutter/src/painting/_network_image_io.dart:84:41)

1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:47:14)

2 ImageProvider.resolve...(包:flutter/src/painting/image_provider.dart:327:17)

3 ImageCache.putIfAbsent (包:flutter/src/painting/image_cache.dart:160:22)

...图片提供者:NetworkImage("http://127.0.0.1:8000/14176.jpg ", scale: 1.0)图片键:NetworkImage("http://127.0.0.1:8000/14176.jpg ", scale: 1.0)

════════ 图片资源服务捕获异常════════════════════════════════════════════ ═══════════════操作系统错误:连接被拒绝,errno = 111,地址 = 127.0.0.1,端口 = 48460

我的代码: slider 服务

class SliderService {
String _baseUrl = 'http://10.0.2.2:8000/api';
getAllSlider() async {
return await httpGet('sliders');
}
httpGet(String api)async{
return await http.get(_baseUrl + '/' + api,headers: {"Accept": "application/json"} );
}
}

主屏幕代码:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:storeapp/services/slider_service.dart';
import 'package:storeapp/widgets/carousel_slider.dart';

class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
SliderService _sliderService = SliderService();
var items=[];
@override
void initState() {
// TODO: implement initState
super.initState();
_getSliders();
}
_getSliders() async {
var sliders = await _sliderService.getAllSlider();
var result = json.decode(sliders.body);

result['data'].forEach((data){
setState(() {
items.add(
NetworkImage(data['image_url']
)
);
//print(data['image_url']);
});
});
}

@override
Widget build(BuildContext context) {
print("Items : $items");
return Scaffold(
appBar: AppBar(
title: Text("MY STORE"),
centerTitle: true,
),
body: Container(
height: 450,
child: ListView(
children: <Widget>[
carouselSlider(items),


),
],
),
),
);
}
}

我怎样才能解决这个问题并在没有任何错误的情况下显示来自本地主机的图像

最佳答案

您是否在 Android list 文件中为 debug 和 main 授予了 internet 权限。

已检查此插件。

https://pub.dev/packages/cached_network_image

使用此代码检查是否有效

CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => new CircularProgressIndicator(),
errorWidget: (context, url, error) => new Icon(Icons.error),
),

关于flutter - Dart/flutter : How can I display Images from API on localhost in emulator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61542473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com