Android Get Api在Emulator中工作,但在真实设备中不起作用 ════════ widgets库捕获到异常═══════════════════════════════════════════════════════ 类型“StateError”不是类型“String”的子类型 导致错误的相关小部件是: 未来建设者file:///C:/Users/Personal/Desktop/park_parcel/lib/Json%20Service/json_Delivery_Data.dart:31:14 ════════════════════════════════════════════════════════════════════════════════════════════════════
这是一个例外/错误,我必须从服务器获取Json数据,它必须通过堆栈中的future.builder绑定到listview.builderAndroid Get Api在Emulator中工作,但在真实设备中不起作用 ════════ widgets库捕获到异常═══════════════════════════════════════════════════════ 类型“StateError”不是类型“String”的子类型 导致错误的相关小部件是: 未来建设者file:///C:/Users/Personal/Desktop/park_parcel/lib/Json%20Service/json_Delivery_Data.dart:31:14 ════════════════════════════════════════════════════════════════════════════════════════════════════,android,json,flutter,android-emulator,flutter-futurebuilder,Android,Json,Flutter,Android Emulator,Flutter Futurebuilder,这是一个例外/错误,我必须从服务器获取Json数据,它必须通过堆栈中的future.builder绑定到listview.builder ════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ type 'StateError' is not a subtype of type 'String' The relevant error-causing w
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
type 'StateError' is not a subtype of type 'String'
The relevant error-causing widget was:
FutureBuilder<List<User>> file:///C:/Users/Personal/Desktop/park_parcel/lib/Json%20Service/json_Delivery_Data.dart:31:14
════════════════════════════════════════════════════════════════════════════════════════════════════
堆栈(
儿童:[
容器(
高度:_高度*1,
宽度:_宽度*1,
子项:DeliveryBody(),
),
定位(
右:_宽度/8,
底部:_高度/5,
子:容器(
高度:_高度*0.18,
页边距:仅限边集(右:20),
对齐:对齐。右下角,
子:RaisedButton.icon(
形状:圆形矩形边框(
边界半径:边界半径。圆形(30.0),
),
颜色:颜色。绿色[600],
图标:图标(Icons.camera\u alt,颜色:Colors.white),
标签:文本(
“扫描交付”,
样式:TextStyle(
颜色:颜色,白色,
),
),
已按下:(){
_scanQR();
},
),
),
),
我的堆栈到此结束,然后我有下面的代码从新的页面
Stack(
children: <Widget>[
Container(
height: _height * 1,
width: _width * 1,
child: DeliveryBody(),
),
Positioned(
right: _width / 8,
bottom: _height / 5,
child: Container(
height: _height * 0.18,
margin: EdgeInsets.only(right: 20),
alignment: Alignment.bottomRight,
child: RaisedButton.icon(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: Colors.green[600],
icon: Icon(Icons.camera_alt, color: Colors.white),
label: Text(
'Scan Deliver',
style: TextStyle(
color: Colors.white,
),
),
onPressed: () {
_scanQR();
},
),
),
),
Future\u getData()异步{
var response=wait http.get(Uri.encodeFull(url));
打印(响应.正文);
如果(response.statusCode==200){
返回计算(parcelList,response.body);
}否则{
抛出异常(“无法从REST API获取产品”);
}
}
FutureOr parcelList(字符串响应库){
//未来值或表示未来值或列表值
var parsed=jsonDecode(responseBody.cast();
返回parsed.map((json)=>User.fromJson(json)).toList();
}
类DeliveryBody扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回容器(
孩子:未来建设者(
未来:_getData(),
生成器:(上下文,快照){
如果(snapshot.hasError)打印(文本(snapshot.error));
返回snapshot.hasData?包裹(项目:snapshot.data):NoParcel();
},
),
);
}
}
类Parcels扩展无状态小部件{
最后清单项目;
const Parcels({Key,this.item}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
final _height=MediaQuery.of(context).size.height-
MediaQuery.of(context.padding.top)-
ktoolbar高度;
final _width=MediaQuery.of(context).size.width;
返回生成器(
生成器:(上下文)=>容器(
高度:_高度*1,
宽度:_宽度*1,
子项:项!=null
?ListView.builder(
itemCount:item.length,
itemBuilder:(构建上下文,int索引){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
列表砖(
tileColor:颜色。绿色[600],
标题:正文(
项[index].id.toString(),
样式:TextStyle(
fontWeight:fontWeight.w900,
颜色:颜色,白色,
字体大小:25.0),
),
),
列表砖(
onTap:(){
返回列(
儿童:[
容器(
高度:_高度/10,
宽度:_宽度*1,
颜色:颜色,白色,
边距:仅限边缘组(顶部:_高度/20),
子:文本(
“包裹详情”,
textAlign:textAlign.center,
样式:TextStyle(
字体大小:_高度/20,
装饰颜色:颜色。黑色,
textb基线:textb基线。字母,
),
),
),
容器(
高度:_高度/10,
宽度:_宽度*1,
颜色:颜色,白色,
边距:仅限边缘组(顶部:_高度/20),
子:文本(
项[index].id.toString(),
textAlign:textAlign.center,
样式:TextStyle(
字体大小:_高度/20,
装饰颜色:颜色。黑色,
Future<List<User>> _getData() async {
var response = await http.get(Uri.encodeFull(url));
print(response.body);
if (response.statusCode == 200) {
return compute(parcelList, response.body);
} else {
throw Exception('Unable to fetch products from the REST API');
}
}
FutureOr<List<User>> parcelList(String responseBody) {
//FutureOr mean Either Future values or List values
var parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<User>((json) => User.fromJson(json)).toList();
}
class DeliveryBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder<List<User>>(
future: _getData(),
builder: (context, snapshot) {
if (snapshot.hasError) print(Text(snapshot.error));
return snapshot.hasData ? Parcels(item: snapshot.data) : NoParcel();
},
),
);
}
}
class Parcels extends StatelessWidget {
final List<User> item;
const Parcels({Key key, this.item}) : super(key: key);
@override
Widget build(BuildContext context) {
final _height = MediaQuery.of(context).size.height -
MediaQuery.of(context).padding.top -
kToolbarHeight;
final _width = MediaQuery.of(context).size.width;
return Builder(
builder: (context) => Container(
height: _height * 1,
width: _width * 1,
child: item != null
? ListView.builder(
itemCount: item.length,
itemBuilder: (BuildContext context, int index) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
tileColor: Colors.green[600],
title: Text(
item[index].id.toString(),
style: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.white,
fontSize: 25.0),
),
),
ListTile(
onTap: () {
return Column(
children: <Widget>[
Container(
height: _height / 10,
width: _width * 1,
color: Colors.white,
margin: EdgeInsets.only(top: _height / 20),
child: Text(
'Parcel Details',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: _height / 20,
decorationColor: Colors.black,
textBaseline: TextBaseline.alphabetic,
),
),
),
Container(
height: _height / 10,
width: _width * 1,
color: Colors.white,
margin: EdgeInsets.only(top: _height / 20),
child: Text(
item[index].id.toString(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: _height / 20,
decorationColor: Colors.black,
textBaseline: TextBaseline.alphabetic,
),
),
),
],
);
},
tileColor: Colors.white,
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 7.0,
),
Text(
"To: " + item[index].name,
style: TextStyle(
fontWeight: FontWeight.w800, fontSize: 30.0),
),
SizedBox(
height: 6.0,
),
Text(item[index].parcelNumber.toUpperCase(),
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 24.0)),
SizedBox(
height: 7.0,
),
Text(item[index].address,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20.2)),
SizedBox(
height: 7.0,
),
Text("Parcels Size:" + item[index].parcelSize,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20.0)),
SizedBox(
height: 7.0,
),
],
),
trailing: Icon(
Icons.arrow_forward_ios,
size: 18.0,
),
),
],
);
},
)
: NoParcel(),
),
);
}
}