Flutter 获取错误--键入';SocketException';不是类型为';小部件';飘飘然
我的应用程序在笔记本电脑上正常工作,这意味着在模拟器中,但在android手机上无法正常工作。当我添加了一个JSON连接时,这个错误就开始了。请帮我解决这个问题 Android屏幕截图(在Android中不起作用) IOS工作屏幕截图(在IOS中工作) 有人告诉我添加下面的代码Flutter 获取错误--键入';SocketException';不是类型为';小部件';飘飘然,flutter,dart,flutter-layout,flutter-dependencies,Flutter,Dart,Flutter Layout,Flutter Dependencies,我的应用程序在笔记本电脑上正常工作,这意味着在模拟器中,但在android手机上无法正常工作。当我添加了一个JSON连接时,这个错误就开始了。请帮我解决这个问题 Android屏幕截图(在Android中不起作用) IOS工作屏幕截图(在IOS中工作) 有人告诉我添加下面的代码 static const Map<String, String> header = { 'Content-type': 'application/json', 'Accept': 'app
static const Map<String, String> header = {
'Content-type': 'application/json',
'Accept': 'application/json',
};
静态常量映射头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
};
我试过了,但还是不走运
import 'package:flutter/material.dart';
import 'package:xxxxxxxx/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();
}
导入“包装:颤振/材料.省道”;
导入“包装:xxxxxxxx/product_page.dart”;
导入“主页_banner.dart”;
将“package:http/http.dart”导入为http;
导入“dart:convert”;
导入'categoryJson.dart';
导入“mCategory.dart”;
导入“featuredItemJSON.dart”;
类HomeScreenBanner扩展了无状态小部件{
静态常量映射头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
};
Future fetchPosts()异步{
http.Response-Response=wait-http
.get('http://api-url-here');
var responseJson=json.decode(response.body);
return(responseJson as List).map((p)=>CategoryAPI.fromJson(p)).toList();
}
未来的mfetchPosts()异步{
http.Response-Response=wait-http
.get('http://api-url-here');
var mresponseJson=json.decode(response.body);
return(mresponseJson as List).map((p)=>MiddleCategoryAPI.fromJson(p)).toList();
}
将来的ffetchPosts()异步{
http.Response-Response=wait-http
.get('http://api-url-here');
var fresponseJson=json.decode(response.body);
return(fresponseJson作为列表).map((p)=>FeaturedItemAPI.fromJson(p)).toList();
}
@凌驾
void initState()异步{
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
],
),
);
}
}
final scrollingofferbanner=HomePageBanner();
@凌驾
小部件构建(构建上下文){
返回容器(
子:新列表视图(
儿童:[
容器(
身高:139.0,
边距:仅限边集(左:0,右:0,顶部:5,底部:0),
填充:仅限边集(顶部:0),
对齐:对齐.center,
颜色:颜色(0xFFFEFF),
子:新列表视图(
滚动方向:轴水平,
儿童:[
容器(
孩子:新未来建设者(
future:fetchPosts(),
生成器:(上下文,快照){
if(snapshot.hasData){
List posts=snapshot.data;
返回新行(
子项:posts.map((post)=>新列(
儿童:[
//类别循环开始
填充物(
填充:常数边集全部(9.0),
儿童:新的手势检测器(
onTap:(){
//更改下面容器的颜色
导航器.of(上下文).push(
新材料路线(
生成器:(上下文)=>new ProductPage(
//producttitle:post.title,
)
)
);
},
子:列(
儿童:[
容器(
宽度:60.0,
身高:60.0,
子:列(
儿童:[
圆形(
半径:30.0,
背景图片:
NetworkImage(post.productimg),
背景颜色:颜色。透明,
),
],
),
),
容器(
宽度:100.0,
页边距:仅限边距(
左:0,右:0,顶部:13,底部:1),
子:列(
儿童:[
容器(
子项:文本(post.title,
textAlign:textAlign.center,
样式:TextStyle(
字体大小:13.0,
fontFamily:“avenirblack”,
颜色:颜色(0xff535353),
)),
)
],
),
),
],
),
),
),
//类别圆端点
],
))托利斯先生()
<uses-permission android:name="android.permission.INTERNET"/>