遇到错误-在颤抖中,“ SocketException”类型不是“ widget”类型的子类型

时间:2019-08-10 13:51:27

标签: flutter dart flutter-layout flutter-dependencies

“我的应用”在笔记本电脑中正常运行意味着在模拟器中运行,但在Android手机中无法运行。当我添加JSON连接时,此错误开始。请帮助我解决它。

Android屏幕截图(在android中不起作用)

SNAP OF THE ERROR

IOS工作屏幕截图(在ios中工作)

SNAP OF THE ERROR

有人告诉我添加以下代码

static const Map<String, String> header = {
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };

尝试了这个,但还是没有运气。

import 'package:flutter/material.dart';
import 'package:hollandi_main/product_page.dart';
import 'homepage_banner.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'categoryJson.dart';
import 'mCategory.dart';
import 'featuredItemJSON.dart';

class HomeScreenBanner extends StatelessWidget {
  static const Map<String, String> header = {
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };

  Future<List<CategoryAPI>> fetchPosts() async {
    http.Response response = await http
        .get('http://api-url-here');
    var responseJson = json.decode(response.body);
    return (responseJson as List).map((p) => CategoryAPI.fromJson(p)).toList();
  }

  Future<List<MiddleCategoryAPI>> mfetchPosts() async {
    http.Response response = await http
        .get('http://api-url-here');
    var mresponseJson = json.decode(response.body);
    return (mresponseJson as List).map((p) => MiddleCategoryAPI.fromJson(p)).toList();
  }

  Future<List<FeaturedItemAPI>> ffetchPosts() async {
    http.Response response = await http
        .get('http://api-url-here');
    var fresponseJson = json.decode(response.body);
    return (fresponseJson as List).map((p) => FeaturedItemAPI.fromJson(p)).toList();
  }

  @override
  void initState() async {
    fetchPosts();
    mfetchPosts();
    ffetchPosts();
  }

根据要求构建方法:

final scrollingofferbanner = HomePageBanner();
@override
  Widget build(BuildContext context) {
    return Container(
      child: new ListView(
        children: <Widget>[
          Container(
            height: 139.0,
            margin: EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: EdgeInsets.only(top: 0),
            alignment: Alignment.center,
            color: Color(0xfffefeff),
            child: new ListView(
              scrollDirection: Axis.horizontal,
              children: <Widget>[
                Container(
                    child: new FutureBuilder<List<CategoryAPI>>(
                    future: fetchPosts(),
                    builder: (context, snapshot) {
                      if (snapshot.hasData){
                        List<CategoryAPI> posts = snapshot.data;
                        return new Row(
                            children: posts.map((post) => new Column(
                              children: <Widget>[
                                //category circle starts
                                Padding(
                                  padding: const EdgeInsets.all(9.0),
                                  child: new GestureDetector(
                                    onTap: () {
                                      // Change the color of the container beneath
                                      Navigator.of(context).push(
                                        new MaterialPageRoute(
                                          builder: (context) => new ProductPage(
                                            //producttitle: post.title,
                                          )
                                        )
                                      );
                                    },
                                    child: Column(
                                      children: <Widget>[
                                        Container(
                                          width: 60.0,
                                          height: 60.0,
                                          child: Column(
                                            children: <Widget>[
                                              CircleAvatar(
                                                radius: 30.0,
                                                backgroundImage:
                                                NetworkImage(post.productimg),
                                                backgroundColor: Colors.transparent,
                                              ),
                                            ],
                                          ),
                                        ),
                                        Container(
                                          width: 100.0,
                                          margin: EdgeInsets.only(
                                              left: 0, right: 0, top: 13, bottom: 1),
                                          child: Column(
                                            children: <Widget>[
                                              Container(
                                                child: Text(post.title,
                                                    textAlign: TextAlign.center,
                                                    style: TextStyle(
                                                      fontSize: 13.0,
                                                      fontFamily: 'avenirblack',
                                                      color: Color(0xff535353),
                                                    )),
                                              )
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                                //category circle ends
                              ],
                            )).toList()
                        );
                      }
                      else if(snapshot.hasError)
                      {
                        return snapshot.error;
                      }
                      return new Center(
                        child: new Column(
                          children: <Widget>[
                            new Padding(padding: new EdgeInsets.all(50.0)),
                            new CircularProgressIndicator(),
                          ],
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
          //top category menu starts
          //top category menu ends
          scrollingofferbanner,
          //middle category menu starts
          new Container(
            height: 188.00,
            margin: EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: EdgeInsets.only(top: 0),
            alignment: Alignment.center,
            color: Color(0xfffefeff),
            child: new ListView(
              scrollDirection: Axis.horizontal,
              children: <Widget>[
                Container(
                  child: new FutureBuilder<List<MiddleCategoryAPI>>(
                    future: mfetchPosts(),
                    builder: (context, snapshot) {
                      if (snapshot.hasData){
                        List<MiddleCategoryAPI> posts = snapshot.data;
                        return new Row(
                            children: posts.map((post) => new Column(
                              children: <Widget>[
                                //category circle starts
                                Padding(
                                  padding: const EdgeInsets.all(9.0),
                                  child: Container(
                                    width: 128.0,
                                    child: Center(
                                      child: Column(
                                        children: <Widget>[
                                          Container(
                                            child: FadeInImage.assetNetwork(
                                              placeholder: 'assets/loading.gif',
                                              image: post.imagen,
                                            ),
                                          ),
                                          //new Image.network(post.imagen),
                                          Container(
                                              margin: EdgeInsets.only(
                                                  left: 0, right: 0, top: 5, bottom: 1),
                                              child: Text(post.title,
                                                  textAlign: TextAlign.center,
                                                  style: TextStyle(
                                                      fontSize: 15.0,
                                                      fontFamily: 'avenirblack',
                                                      color: Color(0xff535353))))
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                                //category circle ends
                              ],
                            )).toList()
                        );
                      }
                      else if(snapshot.hasError)
                      {
                        return snapshot.error;
                      }
                      return new Center(
                        child: new Column(
                          children: <Widget>[
                            new Padding(padding: new EdgeInsets.all(50.0)),
                            new CircularProgressIndicator(),
                          ],
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          )
          //middle category menu ends
          ,
          //featured product list starts
          new Container(
            height: 300.0,
            color: Color(0xffF1ECE7),
            child: new Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(20, 15, 0, 5),
                  child: new Row(
                    children: <Widget>[
                      Container(
                        child: Text('FEATURED ITEMS',
                            style: TextStyle(
                                color: Color(0xffE18C21), fontSize: 20.0)),
                      ),
                    ],
                  ),
                ),
                Container(
                  height: 250.00,
                  margin:
                      EdgeInsets.only(left: 10, right: 0, top: 0, bottom: 0),
                  padding: EdgeInsets.only(top: 0),
                  alignment: Alignment.center,
                  child: new ListView(
                    scrollDirection: Axis.horizontal,
                    children: <Widget>[
                      //category circle starts
                      Container(
                        child: new FutureBuilder<List<FeaturedItemAPI>>(
                          future: ffetchPosts(),
                          builder: (context, snapshot) {
                            if (snapshot.hasData){
                              List<FeaturedItemAPI> posts = snapshot.data;
                              return new Row(
                                  children: posts.map((post) => new Column(
                                    children: <Widget>[
                                      //category circle starts
                                      Padding(
                                        padding: const EdgeInsets.all(9.0),
                                        child: Container(
                                          width: 180.0,
                                          child: Center(
                                            child: new GestureDetector(
                                              onTap: () {
                                                // Change the color of the container beneath
                                                Navigator.of(context).push(
                                                    new MaterialPageRoute(
                                                        builder: (context) => new ProductPage(
                                                          ProductTitle: post.name,
                                                          ProductPrice: post.productprice,
                                                          ProductCode: post.productcode,
                                                          ProductDescription: post.productdescription,
                                                          ProductImage: post.productimg,
                                                          ProductAltTag: post.alttags,
                                                          Productid: post.id,
                                                        )
                                                    )
                                                );
                                              },
                                              child: new Column(
                                                children: <Widget>[
                                                   Container(
                                                     child: FadeInImage.assetNetwork(
                                                       placeholder: 'assets/loading.gif',
                                                       image: post.productimg,
                                                     ),
                                                   ),
                                                   Container(
                                                     margin: EdgeInsets.only(top: 10),
                                                     child: new Text(post.name),
                                                   )
//                                                  Image.network(
//                                                  post.productimg, // On click should redirect to an URL
//                                                  )
                                                ],
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                      //category circle ends
                                    ],
                                  )).toList()
                              );
                            }
                            else if(snapshot.hasError)
                            {
                              return snapshot.error;
                            }
                            return new Center(
                              child: new Column(
                                children: <Widget>[
                                  new Padding(padding: new EdgeInsets.all(50.0)),
                                  new CircularProgressIndicator(),
                                ],
                              ),
                            );
                          },
                        ),
                      )
                      //category circle ends
                    ],
                  ),
                )
              ],
            ),
          ),
          //featured product list ends
        ],
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

其中一种获取方法(也许1+)中存在例外。因此,因此,HomeScreenBanner的输出是一个例外,而不是小部件。

代码中没有小部件构建方法。但是,由于您说它正在模拟器上运行,所以我假设您尚未在此处发布构建方法。

尝试添加一个try catch块,并检查哪些获取方法具有异常。一组打印语句也可能有帮助。

答案 1 :(得分:0)

实际上,我不知道为什么会出现此问题,但我在某处已阅读并找到了此解决方案:

您需要添加

android / app / src / main AndroidManifest.xml 文件中,您需要在标记中添加此权限。

<uses-permission android:name="android.permission.INTERNET"/>