当我想在localhost上显示来自我的API的图像模拟器的所有文本时,我遇到了一个问题,当我经过图像URL模拟器浏览器时,它也会显示,但当我在应用程序小部件(NetworkImage(imageUrl))内显示图像时,我遇到了以下异常
Exception caught by image resource service ════════════════════════════════════════════════ The following SocketException was thrown resolving an image codec: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 48458
抛出异常时,堆栈如下所示:
0网络图像._loadAsync(软件包:flutter/源代码/painting/网络图像. dart文件夹:84:41)
1网络图像. load(软件包:flutter/src/painting/网络图像io. dart:47:14)
2图像提供器...(软件包:/src/painting/图像提供器. dart:327:17)
3图片缓存. putIfAbsent(软件包:flutter/源代码/绘画/图片缓存. dart:160:22)
...图像提供程序:网络映像("http://127.0.0.1:8000/14176.jpg",比例:1.0)图像密钥:网络映像("http://127.0.0.1:8000/14176.jpg",比例:1.0)
════════ Exception caught by image resource service ════════════════════════════════════════════════ OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 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),
),
],
),
),
);
}
}
我如何解决这个问题并显示来自localhost的图像而不出错
2条答案
按热度按时间hivapdat1#
您是否已在Android清单文件中为调试和main提供互联网权限。
已检查此插件。
使用此代码 checkout 是否有效
olhwl3o22#
我有同样的问题,我解决了它把我的电脑的ip地址