過去我曾多次面臨類似的空安全問題,但這次我無法解決。Vscode 說:
The body might complete normally, causing 'null' to be returned
但回傳型別可能是不可為空的型別。對于 ItemBuilder (ListviewBuilder):
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.grey[200],
body: FutureBuilder<AdBanner>(
future: _futureAd,
builder: (BuildContext context, AsyncSnapshot<AdBanner> snapshot) {
if (snapshot.hasData) {
final advertisements = snapshot.data!.data;
return ListView.builder(
itemCount: advertisements!.length,
itemBuilder: (BuildContext context, int index) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CategoryViewDetail(
jsondata: advertisements,
tabbarenable: true,
)));
});
// BannerListTile(advertisements, index, context));
} else if (snapshot.hasError) {
return NewsError(
errorMessage: '${snapshot.hasError}',
);
} else {
return const NewsLoading(
text: 'Loading...',
);
}
},
),
);
}
}
uj5u.com熱心網友回復:
你只是在你的ListView.builder. 它應該是這樣的:
ListView.builder(
itemCount: advertisements!.length,
itemBuilder: (BuildContext context, int index) {
// Fix your previous code here with your widget.
return Text("Something"):
});
uj5u.com熱心網友回復:
嘗試這樣使用:
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.grey[200],
body: FutureBuilder<AdBanner>(
future: _futureAd,
builder: (BuildContext context, AsyncSnapshot<AdBanner> snapshot) {
if (snapshot.hasData) {
final advertisements = snapshot.data!.data;
return ListView.builder(
itemCount: advertisements!.length,
itemBuilder: (BuildContext context, int index) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CategoryViewDetail(
jsondata: advertisements,
tabbarenable: true,
)));
});
// BannerListTile(advertisements, index, context));
}
if (snapshot.hasError) {
return NewsError(
errorMessage: '${snapshot.hasError}',
);
}
return const NewsLoading(
text: 'Loading...',
);
},
),
);
}
}
如果這不起作用,請嘗試在您未來的構建器上添加空檢查
FutureBuilder<AdBanner>?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/350627.html
下一篇:從dart中的陣列中獲取元素
