[英]Flutter pass json data to new Stateful Widget
我将 json 数据传递给 flutter 中的新状态小部件。 我可以在调试控制台中看到数据不是 null 但有状态小部件接收 null 数据。
我尝试了其他传递数据类型,但仍然无法正常工作。
我怎样才能解决这个问题? 任何帮助
这是代码
导航器代码
void goToFilteredList() async {
jsonData =await postQuotationFilteredList(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
false,
false,
null,
null);
print(jsonData);
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FilteredQuotationList(jsonData)));
}
有状态的小部件
class FilteredQuotationList extends StatefulWidget {
final List<Map<String, dynamic>> jsonData;
FilteredQuotationList(this.jsonData);
static const String routeName = "/filteredquotationlist";
@override
State<StatefulWidget> createState() =>
FilteredQuotationListScreen(this.jsonData);
}
jsonData 具有相同的类型并且有缺点,此代码接收 null 数据。
class FilteredQuotationListScreen extends State<FilteredQuotationList> {
List<Map<String, dynamic>> jsonData;
FilteredQuotationListScreen(List<Map<String, dynamic>> jsonData) {
this.jsonData = jsonData;
}
@override
Widget build(BuildContext context) {
print("filtreleme ekranı");
print(jsonData);
return Scaffold(
body: quotationFilteredListItems(jsonData),
);
}
ListView quotationFilteredListItems(List<Map<String, dynamic>> jsonData) {
print("quotation filtered list items");
List<GetQuotationModel> getQuotationModel =
GetQuotationModel.fromJson(jsonData);
print(getQuotationModel.length);
return ListView.builder(
itemCount: 3,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.amberAccent,
elevation: 2.0,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.green,
child: Text("getQuotationModel.quoteNumber"),
),
title: Text("this."),
),
);
},
);
}
}
首先,这不是您应该使用通过构造函数提供数据的有状态小部件的方式。 基本上,State object 可以免费访问 Widget object 的任何参数。
这是一个应该如何使用它的例子。
class MyWidget extends StatefulWidget {
MyWidget({
Key key,
@required this.myParameter,
}) : super(key: key);
final String myParameter;
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
return Text(widget.myParameter); // the widget accessor is included in every State
}
}
然后使用它看起来像这样:
@override
Widget build(BuildContext context) {
return MyWidget(myParameter: "Hurray!");
}
让我知道这是否解决了您遇到的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.