如何使用具有空安全性的 Future.wait 在 Futurebuilder 中获得第一个 Future
How to get first Future in Futurebuilder using Future.wait with null-safety
我有一个 Futurebuilder,我在其中调用多个期货。我想获得第一个未来数据,但我找不到一种方法来告诉 null-safety 这不能为 null。
FutureBuilder(
future:
Future.wait([contactInformationController.companiesFuture.value]),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.active:
case ConnectionState.waiting:
return SizedBox(height: 600, child: LoadingIndicator());
case ConnectionState.done:
if (snapshot.hasError) {
return SizedBox(
width: double.infinity,
height: 600,
child: Center(
child: Text("fetcherror".tr,
style: Theme.of(context).textTheme.subtitle1!)));
}
contactInformationController.companiesData.value = snapshot.data[0]!;
...
它给我这个错误:
The method '[]' can't be unconditionally invoked because the receiver
can be 'null'.
如何使此代码在空安全的情况下工作:
contactInformationController.companiesData.value = snapshot.data[0]!;
我修好了。使用 Future.wait 时。快照 returns 对象数据。你需要转换它然后调用它。
像这样:
contactInformationController.companiesData.value = (snapshot.data! as List)[0]
我有一个 Futurebuilder,我在其中调用多个期货。我想获得第一个未来数据,但我找不到一种方法来告诉 null-safety 这不能为 null。
FutureBuilder(
future:
Future.wait([contactInformationController.companiesFuture.value]),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.active:
case ConnectionState.waiting:
return SizedBox(height: 600, child: LoadingIndicator());
case ConnectionState.done:
if (snapshot.hasError) {
return SizedBox(
width: double.infinity,
height: 600,
child: Center(
child: Text("fetcherror".tr,
style: Theme.of(context).textTheme.subtitle1!)));
}
contactInformationController.companiesData.value = snapshot.data[0]!;
...
它给我这个错误:
The method '[]' can't be unconditionally invoked because the receiver can be 'null'.
如何使此代码在空安全的情况下工作:
contactInformationController.companiesData.value = snapshot.data[0]!;
我修好了。使用 Future.wait 时。快照 returns 对象数据。你需要转换它然后调用它。
像这样:
contactInformationController.companiesData.value = (snapshot.data! as List)[0]