我无法访问内部for循环中的外部for循环计数器
关于如何执行此操作的任何想法?
class buildsubcategories extends StatelessWidget {
List<cate.Categories> scat;
buildsubcategories(this.scat);
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
for (int i = 0; i < scat.length; i++) Text(scat[i].categoryname),
Column(
children: <Widget>[
for (int j = 0; j < scat.length; j++)
Text(scat[i].subcategory[j]['subcategoryname'].toString())
],
)
],
);
}
```}
Expected Result : Able to access the variable i in the inner for loop
答案 0 :(得分:2)
您在这里没有嵌套循环。请查看我在下面添加的评论:
children: <Widget>[
// note the ... spread operator that enables us to add two elements
for (int i = 0; i < scat.length; i++) ...[
Text(scat[i].categoryname),
Column(
children: <Widget>[
// this creates scat.length many elements inside the Column
for (int j = 0; j < scat.length; j++)
Text(scat[i].subcategory[j]['subcategoryname'].toString())
],
)
]
],
以下是在嵌套循环中创建类别的方法:
...
请注意,要在每个循环迭代中添加两个元素,我们必须将两个元素放入列表中,并使用 public async Task<IActionResult> LoginConfirm(LoginViewModel model)
{
ApplicationUser user = await userManager.FindByNameAsync(model.Email);
if (user!=null)
{
var status = await signInManager.PasswordSignInAsync(user, model.Pass,model.RememberMe,true);
if (status.Succeeded)
{
TempData["msg"] = "You Login successful ";
return RedirectToAction("Index","Home");
}
}
TempData["msg"] = "Somethings Wrong!";
return View("Login");
}
扩展运算符对其进行解包。
答案 1 :(得分:0)
当您试图在return语句中创建for循环时,我认为您使用的语法不合适。相反,您可以尝试以下方法:
@override
Widget build(BuildContext context) {
List<Widget> myList = List.generate(10, (index){
var outerText = [Text("OuterText")];
return Column(
children: outerText..addAll(List.generate(10, (index){
Text("Inner Text");
})),
);
});
return Column(
children: myList
);
}