android эмулятор студии не загружает изображение с локального хоста - PullRequest
0 голосов
/ 18 апреля 2020

Привет всем, я программирую на флаттере и использую android студийный эмулятор, но дело в том, что эмулятор загружает не все изображения, и это становится очень раздражающим в процессе, вот картина проблемы

в номере 4 здесь должно быть изображение, но оно не загружается, хотя мой сервер отвечает правильно

Примечание: это страница категорий, если вы хотите знать

Код

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'Coupons.dart';


class CompanyCategories {
  final int id;
  final String title;
  final String icon;

  CompanyCategories({this.id, this.title, this.icon});

  factory CompanyCategories.fromJson(Map<String, dynamic> json) {
    return CompanyCategories(
      id: json['id'],
      title: json['title'],
      icon: json['icon'],
    );
  }
}

Future<List<CompanyCategories>> fetchCategories(int companyId) async {
  final response = await http.get('http://192.168.1.20:8000/categories/api/fetch/$companyId');
  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return parseCategories(response.body);
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load Categories');
  }
}

List<CompanyCategories> parseCategories(String responseBody) {
  final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();

  return parsed.map<CompanyCategories>((json) => CompanyCategories.fromJson(json)).toList();
}


class Categories extends StatefulWidget {
  //getting company id from home page
  final int companyId;
  final companyName;
  Categories(this.companyId , this.companyName);
  @override
  _CategoriesState createState() => _CategoriesState();
}

class _CategoriesState extends State<Categories> {
  @override
  Widget build(BuildContext context) {
    final Future<List<CompanyCategories>> futureCategories = fetchCategories(widget.companyId);

    return Scaffold(
      appBar: AppBar(
        bottomOpacity: 0.0,
        elevation: 0.0,
       backgroundColor: Colors.cyan,
       title: Center(
         child: Text('التصنيفات' , style: TextStyle(color: Colors.white),),
       ),
      ),
      body: Center(
        child : FutureBuilder<List<CompanyCategories>>(
          future: futureCategories,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              List<CompanyCategories> data = snapshot.data;
              return Container(
                color: Colors.cyan,
                child: Container (
                  margin: EdgeInsets.only(bottom: 20.0),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      shape: BoxShape.rectangle,
                      borderRadius: BorderRadius.circular(30),
                  ),
                  child : GridView.count(
                    crossAxisCount: 2,
                    // Generate 100 widgets that display their index in the List.
                    children: List.generate(data.length, (index) {
                      return GestureDetector(
                        onTap: (){
                          Navigator.push(
                            context,
                            MaterialPageRoute(builder: (context) => Coupons(data[index].id , widget.companyId , widget.companyName)),
                          );
                        },
                        child: Align(
                          alignment: Alignment.center,
                          child :Stack(
                            children: <Widget>[
                              Container(
                                margin: EdgeInsets.all(10),
                                decoration: BoxDecoration(
                                  shape: BoxShape.rectangle,
                                  borderRadius: BorderRadius.circular(15),
                                ),
                                child: Container(
                                  margin: EdgeInsets.all(15),
                                  width: 260,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(20),
                                    shape: BoxShape.rectangle,
                                  ),
                                  child: Image.network('http://192.168.1.20:8000${data[index].icon}' ,
                                  ),
                                ),
                              ),
                              Container(
                                alignment: Alignment.bottomCenter,
                                child: Text('${data[index].title}',
                                  style: TextStyle(
                                    color: Colors.black,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    }),
                  ),
                ),
              );
            } else if (snapshot.hasError) {
              return Text("${snapshot.error}");
            }

            // By default, show a loading spinner.
            return CircularProgressIndicator();
          },
        ),
        ),
      );
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...