【发布时间】:2020-08-20 05:28:36
【问题描述】:
当没有数据时,我想在flutter gridview中显示一个文本小部件(“未找到数据”)。我也使用了嵌套三元运算符,但不起作用。
这是我正在尝试的代码。 创建了一个 gridview 小部件。
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:recipe_app/models/recipe.dart';
import 'package:recipe_app/services/recipe_service.dart';
import 'package:recipe_app/widgets/recipe_by_category.dart';
class RecipesByCategoryName extends StatefulWidget {
final String categoryName;
final String categoryIcon;
final int categoryId;
RecipesByCategoryName(
{this.categoryIcon, this.categoryId, this.categoryName});
@override
_RecipesByCategoryNameState createState() => _RecipesByCategoryNameState();
}
class _RecipesByCategoryNameState extends State<RecipesByCategoryName> {
RecipeService _recipesService = RecipeService();
List<Recipe> _recipeListByCategory = List<Recipe>();
bool isLoading = true;
@override
void initState() {
super.initState();
_getRecipesByCategory();
}
_getRecipesByCategory() async {
var products =
await _recipesService.getRecipesByCategoryId(widget.categoryId);
var _list = json.decode(products.body);
_list["data"].forEach((data) {
var model = Recipe();
model.id = data["id"];
model.title = data["recipeTitle"];
model.image = data["recipePhoto"];
model.cookTime = data["cookTime"].toString();
model.ingredients = data["recipeIngredient"];
model.directions = data["recipeDirection"];
setState(() {
_recipeListByCategory.add(model);
isLoading = false;
});
});
}
Widget getGridView(){
return (_recipeListByCategory?.length != 0) ? GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: _recipeListByCategory.length,
itemBuilder: (context, index) {
return RecipeByCategory(
this._recipeListByCategory[index],
);
},
) : Text("No Data Found");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(this.widget.categoryName)),
body: Container(
child: Center(
child: isLoading
? CircularProgressIndicator(
backgroundColor: Colors.deepPurpleAccent,
strokeWidth: 10,
)
:
getGridView()
),
));
}
}
更新 当没有数据时,我想在颤动的网格视图中显示一个文本小部件(“未找到数据”)。我也使用了嵌套三元运算符,但不起作用。
请查看 isLoading 函数。
【问题讨论】:
-
究竟是什么不起作用?显示正在发生的事情或您有什么错误,也无需将您的文本小部件包装在中心,因为 getGridView 已经在其中
-
可以上传完整代码吗?
-
@EdwynZN 我没有收到任何错误。仅显示 CirculaprogressIndicator。我无法显示空短信
-
isLoading 永远不会是假的,在你的情况下,检查它的值,当它变为真时也看看
-
可以上传完整代码吗?