我未来的构建器小部件保留 returns 空值?
My future builder widget keep returns null value?
这是我的 Train 客户端 class 和 getName 方法 return 是我的 Train 对象列表。我通过使用打印语句检查它是否成功运行,但是当我 returning 时我不确定它是否 returning 正确
class TrainClient {
static Future<List<Train>> getName() async {
final uri = Uri.parse("http://localhost:3000/search?from=54&to=61&date=2021-08-11");
final response = await get(uri);
final data = json.decode(response.body);
final result = data["RESULTS"]["directTrains"]["trainsList"];
final list = result.map((json) => Train.fromJson(json)).toList();
print("TrainClient");
print(list);
return list;
}
}
调用此方法时得到输出
[[=25= 的实例],[=25= 的实例],[=25= 的实例],[=25= 的实例],[=25= 的实例],[= 的实例25=], 'Train' 的实例, 'Train' 的实例, 'Train' 的实例
'Train'、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[= 的实例25=], 'Train' 的实例, 'Train' 的实例,
'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例'Train', 'Train' 的实例, 实例
'Train'、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[= 的实例25=], 'Train' 的实例, 'Train' 的实例,
'Train' 的实例,'Train' 的实例,'Train' 的实例,'Train']
的实例
但是当我在这里使用未来的构建器时,我总是得到空值,首先,屏幕是红色的,然后变成绿色,然后什么都没有 returns。但我希望 DataTable 为 return
这是我未来的建设者的代码
class TimeTable extends StatefulWidget {
final url;
const TimeTable({Key? key,required this.url}) : super(key: key);
@override
_TimeTableState createState() => _TimeTableState();
}
class _TimeTableState extends State<TimeTable> {
@override
Widget build(BuildContext context) {
return Scaffold(body:FutureBuilder(
future:TrainClient.getName() ,
builder: (context,AsyncSnapshot<List<Train>> snapshot){
if(snapshot.connectionState==ConnectionState.done){
print(snapshot.data);
if(snapshot.data != null) {
return buildDataTable(trains: snapshot.data!);
}
return Container(color:Colors.green);
}
return Container(color: Colors.red,);
},
) );
}}
我打印的时候也得到了snapshot.data== null。这是为什么???为什么它保持 returning null
使用 dio for api integrations and this json converter 来处理您的回复怎么样?
顺便一提。
每次重建小部件时都会调用 getName 函数。为避免每次重建小部件时调用该函数,您不得在 State.build 或 StatelessWidget.build 方法中创建 Future。您必须更早地在未来构建器之外创建未来。所以创建一个本地 var _listName 和 int State.didChangeDependencies,或者 State.didUpdateWidget.
class TimeTable extends StatefulWidget {
final url;
const TimeTable({Key? key,required this.url}) : super(key: key);
@override
_TimeTableState createState() => _TimeTableState();
}
class _TimeTableState extends State<TimeTable> {
Future<List<Train>> _value;
@override
initState() {
super.initState();
_value = getName();
}
@override
Widget build(BuildContext context) {
return Scaffold(body:FutureBuilder(
future:_value ,
builder: (context,AsyncSnapshot<List<Train>> snapshot){
if(snapshot.connectionState==ConnectionState.done){
print(snapshot.data);
if(snapshot.data != null) {
return buildDataTable(trains: snapshot.data!);
}
return Container(color:Colors.green);
}
return Container(color: Colors.red,);
},
) );
}}
你可以试试这个。它还用于检查快照是否有错误
Widget build(BuildContext context) {
return Scaffold<List<Train>>(body:FutureBuilder(
future:TrainClient.getName() ,
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.hasError){
print(snapshot)
} else
if(snapshot.hasData){
print(snapshot.data);
return buildDataTable(trains: snapshot.data!);
}
return Container(color:Colors.green);
}
return Container(color: Colors.red,);
},
) );
并在 TrainClient
中定义 return 类型
return List<Train>.from(result.map((item) => Train.fromJson(item)));
这是我的 Train 客户端 class 和 getName 方法 return 是我的 Train 对象列表。我通过使用打印语句检查它是否成功运行,但是当我 returning 时我不确定它是否 returning 正确
class TrainClient {
static Future<List<Train>> getName() async {
final uri = Uri.parse("http://localhost:3000/search?from=54&to=61&date=2021-08-11");
final response = await get(uri);
final data = json.decode(response.body);
final result = data["RESULTS"]["directTrains"]["trainsList"];
final list = result.map((json) => Train.fromJson(json)).toList();
print("TrainClient");
print(list);
return list;
}
}
调用此方法时得到输出
[[=25= 的实例],[=25= 的实例],[=25= 的实例],[=25= 的实例],[=25= 的实例],[= 的实例25=], 'Train' 的实例, 'Train' 的实例, 'Train' 的实例 'Train'、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[= 的实例25=], 'Train' 的实例, 'Train' 的实例, 'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例,'Train'的实例'Train', 'Train' 的实例, 实例 'Train'、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[=25= 的实例]、[= 的实例25=], 'Train' 的实例, 'Train' 的实例, 'Train' 的实例,'Train' 的实例,'Train' 的实例,'Train']
的实例但是当我在这里使用未来的构建器时,我总是得到空值,首先,屏幕是红色的,然后变成绿色,然后什么都没有 returns。但我希望 DataTable 为 return
这是我未来的建设者的代码
class TimeTable extends StatefulWidget {
final url;
const TimeTable({Key? key,required this.url}) : super(key: key);
@override
_TimeTableState createState() => _TimeTableState();
}
class _TimeTableState extends State<TimeTable> {
@override
Widget build(BuildContext context) {
return Scaffold(body:FutureBuilder(
future:TrainClient.getName() ,
builder: (context,AsyncSnapshot<List<Train>> snapshot){
if(snapshot.connectionState==ConnectionState.done){
print(snapshot.data);
if(snapshot.data != null) {
return buildDataTable(trains: snapshot.data!);
}
return Container(color:Colors.green);
}
return Container(color: Colors.red,);
},
) );
}}
我打印的时候也得到了snapshot.data== null。这是为什么???为什么它保持 returning null
使用 dio for api integrations and this json converter 来处理您的回复怎么样? 顺便一提。 每次重建小部件时都会调用 getName 函数。为避免每次重建小部件时调用该函数,您不得在 State.build 或 StatelessWidget.build 方法中创建 Future。您必须更早地在未来构建器之外创建未来。所以创建一个本地 var _listName 和 int State.didChangeDependencies,或者 State.didUpdateWidget.
class TimeTable extends StatefulWidget {
final url;
const TimeTable({Key? key,required this.url}) : super(key: key);
@override
_TimeTableState createState() => _TimeTableState();
}
class _TimeTableState extends State<TimeTable> {
Future<List<Train>> _value;
@override
initState() {
super.initState();
_value = getName();
}
@override
Widget build(BuildContext context) {
return Scaffold(body:FutureBuilder(
future:_value ,
builder: (context,AsyncSnapshot<List<Train>> snapshot){
if(snapshot.connectionState==ConnectionState.done){
print(snapshot.data);
if(snapshot.data != null) {
return buildDataTable(trains: snapshot.data!);
}
return Container(color:Colors.green);
}
return Container(color: Colors.red,);
},
) );
}}
你可以试试这个。它还用于检查快照是否有错误
Widget build(BuildContext context) {
return Scaffold<List<Train>>(body:FutureBuilder(
future:TrainClient.getName() ,
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.hasError){
print(snapshot)
} else
if(snapshot.hasData){
print(snapshot.data);
return buildDataTable(trains: snapshot.data!);
}
return Container(color:Colors.green);
}
return Container(color: Colors.red,);
},
) );
并在 TrainClient
中定义 return 类型return List<Train>.from(result.map((item) => Train.fromJson(item)));