flutter 落镖/抖动:如何在模拟器中显示来自本地主机上API的图像

toe95027  于 2022-12-30  发布在  Flutter
关注(0)|答案(2)|浏览(100)

当我想在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的图像而不出错

hivapdat

hivapdat1#

您是否已在Android清单文件中为调试和main提供互联网权限。
已检查此插件。

https://pub.dev/packages/cached_network_image

使用此代码 checkout 是否有效

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

olhwl3o22#

我有同样的问题,我解决了它把我的电脑的ip地址

Image.network('http://192.168.1.65/img/${report.image}')

相关问题