gpt4 book ai didi

android - 如何解决显示垂直视口(viewport)的 flutter 抽屉被赋予无限高度

转载 作者:行者123 更新时间:2023-12-01 16:12:54 26 4
gpt4 key购买 nike

值得一提的是,我对 Flutter 和 Stackoverflow 都是新手。

在我的新闻阅读器应用程序中,我添加了一个侧边抽屉,它使用 FutureBuilder 从 API 中提取新闻类别。有一个DrawerHeader包含名为“热门新闻”的类别,该类别不是从 API 获取的,即静态的。

每次我打开 API 时,热门新闻类别都会显示并且工作正常,但其他类别不会显示,而是在控制台中显示如下错误。

I/flutter ( 4486): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4486): The following assertion was thrown during performResize():
I/flutter ( 4486): Vertical viewport was given unbounded height.
I/flutter ( 4486): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 4486): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 4486): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 4486): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 4486): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 4486): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 4486): the height of the viewport to the sum of the heights of its children.

我的代码如下

返回抽屉的小部件
@override
Widget build(BuildContext context) {
SizeConfig().init(context);

return SizedBox(
width: SizeConfig.safeBlockHorizontal*50,
child: Theme(
data: Theme.of(context).copyWith(canvasColor: const Color(0xFF2b4849)),
child: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: ListTile(
title: Text(
"Popular News",
style: TextStyle(
color: Colors.white,
fontSize: 25
),
),
onTap: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => MostPopularNewsfeed(),
)
);
},
),
),
FutureBuilder(
future: category,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if(snapshot.data == null) {
return Container(
child: Center(
child: Text(
"Loading",
style: TextStyle(
color: Colors.white
),
),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return _getDrawer(snapshot, index);
},
);
}
},
),
],
),
),
),
);
}


初始化状态() : 这个 categoryFuture<List>
void initState() {
super.initState();

setState(() {
category = fetchCategory();
});
}


fetchCategory() :这个从 API 中获取类别。
Future<List<Category>> fetchCategory() async {
//try {
String url = "https://tbsnews.net/json/category/news/list";
dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl: url)).interceptor);
Response response = await Dio().get(url);
print(response);

List<Category> categoryList = [];
final Future<Database> dbFuture = categoryDatabaseHelper.initDb();

if(response.statusCode == 200) {
var decode = response.data;
print(decode);
for (var c in decode) {
Category category = Category(c['tid'], c['name']);
await categoryDatabaseHelper.insertCategory(category);
categoryList.add(category);
}
return categoryList;
//categoryDatabaseHelper.insertCategory(categoryList);
} else {
throw Exception("Failed to fetch category");
}
}


到目前为止我尝试过的

我试过把 DrawerHeaderFutureBuilderExpanded 内小部件而不是 ListView .不工作。

在 ListView 我添加了 shrinkWrap: true , 还有 scrollDirection: Axis.vertical .那也没用。

试图将它们放入 SizeBoxColumn小部件也。那也没有用。

以上是我在 StackOverflow 中搜索之前与此问题相关的问题后发现的。最后,我自己发布一个问题。

需要注意的是,当我拿走 DrawerHeader一切都开始正常工作,不再有麻烦了。但是这个 DrawerHeader必须在那里。

非常感谢您的宝贵时间和真诚的帮助。

最佳答案

如果问题仅出在 DrawerHeader 上,请尝试用 SizedBox 包装它或 ConstrainedBox并给它一定的高度和宽度。
问题是 DrawerHeader不受约束,因此 ListView在构建时不知道其尺寸或约束并引发此错误。

这是我有根据的猜测,让我知道它是否有效。

关于android - 如何解决显示垂直视口(viewport)的 flutter 抽屉被赋予无限高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60321757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com