【问题标题】:Retrive data from laravel API to flutter app从 laravel API 检索数据到颤振应用程序
【发布时间】:2020-05-05 13:15:09
【问题描述】:

我已经在我的颤振应用程序和 laravel api 之间创建了一个连接,但我不知道为什么我无法从后端获取一些数据(图像)以显示在应用程序上。我在本地服务器上运行 laravel api,在 android 模拟器上运行颤振应用程序。

这是我的代码...

来自 Laravel api - CategoriesResource.php

 <?php

    namespace App\Http\Resources;

    use Illuminate\Http\Resources\Json\JsonResource;

    class CategoriesResource extends JsonResource
  {
     /**
        * Transform the resource into an array.
        *
        * @param  \Illuminate\Http\Request  $request
        * @return array
     */
  public function toArray($request)
     {
         return [
              'categoryName' => $this->categoryName,
               'category_image' => 'http://127.0.0.1:8000/api/v1/public/category_images/'.$this->category_image
              ];
     }
  }   

我的颤振代码...

    import 'package:flutter/material.dart';
    import 'package:restaurant_app/models/category.dart';

    class HomeCategoriesGet extends StatefulWidget {
       final List<Category> categoryList;
       final int categoryId;
       final String categoryName;
       final String categoryImage;
      @override
      HomeCategoriesGet(
           {this.categoryList,
           this.categoryId,
           this.categoryName,
           this.categoryImage});
      _HomeCategoriesGetState createState() => _HomeCategoriesGetState();
    }

    class _HomeCategoriesGetState extends State<HomeCategoriesGet> {
         @override
         Widget build(BuildContext context) {
         return ListView.builder(
              itemCount: this.widget.categoryList.length,
              itemBuilder: (context, index) {
                 return Card(
                        child: Column(
                        children: <Widget>[
                           Image.network(
                             // This is where i am trying to get the image
                             this.widget.categoryList[index].catImage,
                             width: 190.0,
                             height: 160.0,
                           ),
                           Padding(
                           padding: const EdgeInsets.all(8.0),
                             child: Text(this.widget.categoryList[index].name),
                           ),
                       ],
                    ),
                  );
                },
              );
             }
           }

错误

    ════════════════════════════ 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 = 48849

   When the exception was thrown, this was the stack

   #0      NetworkImage._loadAsync 
       package:flutter/…/painting/_network_image_io.dart:84
   <asynchronous suspension>

   #1      NetworkImage.load 
       package:flutter/…/painting/_network_image_io.dart:48

   #2      ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> 
       package:flutter/…/painting/image_provider.dart:316

   #3      ImageCache.putIfAbsent 
       package:flutter/…/painting/image_cache.dart:160
   ...

   Image provider: NetworkImage("http://127.0.0.1:8000/api/v1/public/category_images/starters_1578944124.jpeg", scale: 1.0)

   Image key: NetworkImage("http://127.0.0.1:8000/api/v1/public/category_images/starters_1578944124.jpeg", scale: 1.0)

这是我的 Repository.dart 文件

    import 'package:http/http.dart' as http;
        // Create connection with Back-end (laravel)

        class Repository {
           // connection with emulator
           String _baseUrl = 'http://10.0.2.2:8000/api';

           httpGet(String api) async {
               return await http.get(_baseUrl + "/" + api);
          }

           httpGetById(String api, id) async {
               return await http.get(_baseUrl + "/" + api + "/" + id.toString());
          }
        }

请帮我解决这个问题。谢谢

【问题讨论】:

    标签: laravel-5 flutter


    【解决方案1】:

    删除baseUrl变量中的/api

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    【解决方案2】:

    解决这个问题:

    第一次运行php artisan serve --host 0.0.0.0
    然后通过在终端上键入以下命令获取您的本地 IP:
    对于 Windows:ipcofing
    Linux 或 Mac:ifconfig
    就我而言,它是:IPv4 Address . . . . . . . . . . : 192.168.1.122
    最后: `

        class Repository {
           // connection with emulator
           String _baseUrl = 'http://192.168.1.122:8000/api';
          ....
          }
    

    `

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多