【发布时间】:2021-01-22 10:00:14
【问题描述】:
我正在从服务器获取一些产品,但我收到一条错误消息,指出 引发了另一个异常:ParentDataWidget 的使用不正确。
AsyncSnapshot(ConnectionState.waiting, null, type '(dynamic) => ProductsModel' 不是 '(String, dynamic) => MapEntry
Incorrect use of ParentDataWidget.
The ParentDataWidget Flexible(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.
Usually, this means that the Flexible widget has the wrong ancestor RenderObjectWidget. Typically, Flexible widgets are placed directly inside Flex widgets.
The offending Flexible is currently placed inside a RepaintBoundary widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
RichText ← Text ← FutureBuilder<List<ProductsModel>> ← Container ← Flexible ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← ⋯
When the exception was thrown, this was the stack:
#0 RenderObjectElement._updateParentData.<anonymous closure> (package:flutter/src/widgets/framework.dart:5689:11)
#1 RenderObjectElement._updateParentData (package:flutter/src/widgets/framework.dart:5705:6)
#2 ParentDataElement._applyParentData.applyParentDataToChild (package:flutter/src/widgets/framework.dart:4939:15)
#3 ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4600:14)
#4 ParentDataElement._applyParentData.applyParentDataToChild (package:flutter/src/widgets/framework.dart:4942:15)
...
Another exception was thrown: Incorrect use of ParentDataWidget.
AsyncSnapshot<List<ProductsModel>>(ConnectionState.waiting, null, type '(dynamic) => ProductsModel' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform')
这是我的 ProductModel 类
class ProductsModel {
String adid;
String adsubcatid;
String aduserid;
String adname;
String adcoverimg;
String addesc;
String adsPrice;
String adsLocation;
String createdDate;
String userName;
String images;
ProductsModel(
{this.adid,
this.adsubcatid,
this.aduserid,
this.adname,
this.adcoverimg,
this.addesc,
this.adsPrice,
this.adsLocation,
this.createdDate,
this.userName,
this.images});
factory ProductsModel.fromJson(Map<String, dynamic> json) {
return ProductsModel(
adid: json['adid'],
adsubcatid: json['adsubcatid'],
aduserid: json['aduserid'],
adname: json['adname'],
adcoverimg: json['adcoverimg'],
addesc: json['addesc'],
adsPrice: json['ads_price'],
adsLocation: json['ads_location'],
createdDate: json['created_date'],
userName: json['user_name'],
images: json['images']
);
}
}
这是主页
@override
void initState() {
getProducts();
super.initState();
}
@override
Widget build(BuildContext context) {
print("Home");
return Scaffold(
appBar: AppBar(
leading: Image.asset('assets/images/ic_logo.png'),
actions: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: FlatButton.icon(
onPressed: () {},
icon: Icon(Icons.location_on),
label: Text('Mysuru, India'),
textColor: Colors.white,
),
),
],
),
body: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: SizedBox(
child: _search(),
),
),
SliverToBoxAdapter(
child: SizedBox(
child: HorizontalCategories(),
),
),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 0.7,
),
delegate: SliverChildBuilderDelegate(
(context, index) => _products(context)),
)
],
),
);
}
Widget _products(BuildContext context) {
print("Inside products");
return Flexible(
child: Container(
child: FutureBuilder<List<ProductsModel>>(
future: _fetchProducts(),
builder: (context, snapshot) {
if (snapshot.hasData) {
print("Has data");
List<ProductsModel> data = snapshot.data;
return _productsList(data);
} else if (snapshot.hasError) {
print("Error");
print(snapshot.hasError);
print(snapshot);
return Text('${snapshot.hasError}');
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
),
);
}
Future<List<ProductsModel>> _fetchProducts() async {
String productsUrl = Constant.productsUrl;
Map<String, String> headers = {'Content-Type': 'application/json'};
final response = await http.get(productsUrl, headers: headers);
if (response.statusCode == 200) {
var jsonResponse = json.decode(response.body);
print(jsonResponse);
return jsonResponse
.map((products) => new ProductsModel.fromJson(products))
.toList();
} else {
throw Exception('Failed to load Categories from API');
}
}
这是 JsonResponse
{
"status": true,
"record": [
{
"adid": "1",
"adsubcatid": "1",
"aduserid": "2",
"adname": "Ads",
"adcoverimg": "Royal Enfield Classic 350 Images Classic 350 Photos & 360 View_files.JPG",
"addesc": "vsv dsv sd fds fd fdsf dsf s fds fdssfdf c sfdf sd",
"ads_price": "3000",
"ads_location": "mysore",
"created_date": "06/10/2020",
"user_name": "sunil",
"images": "{'adsimg_img':'2.JPG'},{'adsimg_img':'3.JPG'},{'adsimg_img':'4.JPG'}"
}
]
}
【问题讨论】:
-
可以加
jsonResponse吗?示例响应。 -
jsonResponse不是可迭代的(列表),因此无法调用map。 -
弹性小部件只能有弹性小部件(行或列)作为父级。尝试在 _products 方法中移除 Flexible Widget。
-
即使我删除了 Flexible 它在 Futurebuilder 中给我一个错误