Flutter 使用索引生成带有颤振的GridView

Flutter 使用索引生成带有颤振的GridView,flutter,flutter-layout,Flutter,Flutter Layout,我想做的是用上面定义的索引生成一个带有flatter的GridView,但是出于某种原因,它说“未定义的名称索引”,你能帮我一下吗 代码如下: import 'package:flutter/material.dart'; void main() => runApp(MyApp(items: List<String>.generate(1000, (index) => "Item $index"))); class MyApp extends StatelessWid

我想做的是用上面定义的索引生成一个带有flatter的GridView,但是出于某种原因,它说“未定义的名称索引”,你能帮我一下吗

代码如下:

import 'package:flutter/material.dart';

void main() => runApp(MyApp(items: List<String>.generate(1000, (index) => "Item $index")));

class MyApp extends StatelessWidget {

  final List<String> items;

  MyApp({Key key, @required this.items}) : super(key: key);


  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(

          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
            appBar:
            AppBar(title:
            Text('List View Vertical'),),
            body:
              GridView.count(
                  crossAxisCount: 2,
                  children: List.generate(100, index)(
                    return Center(child: Text('Items $index',
                  style: Theme.of(context).textTheme.headline,),);
                  ))
        )

    );
  }
}

导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp(items:List.generate(1000,(index)=>Item$index));
类MyApp扩展了无状态小部件{
最后清单项目;
MyApp({Key-Key,@required this.items}):超级(Key:Key);
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
家:脚手架(
appBar:
AppBar(标题:
文本(‘列表视图垂直’,),
正文:
GridView.count(
交叉轴计数:2,
子项:列表。生成(100,索引)(
返回中心(子项:文本('Items$index',
风格:Theme.of(context.textTheme.headline,),);
))
)
);
}
}
我预期的结果是使用我已经定义的索引生成1000行gridview。

用以下内容替换您的
build()

@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: Scaffold(
      appBar: AppBar(
        title: Text('List View Vertical'),
      ),
      body: GridView.count(
        crossAxisCount: 2,
        children: items.map((title) {
        return Center(
          child: Text(
            '$title',
            style: Theme.of(context).textTheme.headline,
          ),
        );
      }).toList(),
      ),
    ),
  );
}

更改代码以使用您要传递的属性
中的值

例如:

class MyApp extends StatelessWidget {
  final List<String> items;

  MyApp({Key key, @required this.items}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('List View Vertical'),
        ),
        body: GridView.count(
          crossAxisCount: 2,
          children: items.map((text) {
            return Center(
              child: Text(
                '$text',
                style: Theme.of(context).textTheme.headline,
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}
类MyApp扩展了无状态小部件{
最后清单项目;
MyApp({Key-Key,@required this.items}):超级(Key:Key);
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
家:脚手架(
appBar:appBar(
标题:文本(“列表视图垂直”),
),
正文:GridView.count(
交叉轴计数:2,
子项:items.map((文本){
返回中心(
子:文本(
“$text”,
风格:Theme.of(context).textTheme.headline,
),
);
}).toList(),
),
),
);
}
}

希望有帮助

谢谢你的帮助,你的代码工作正常,但不是我想要的方式,如果你能在第二行上面查看我的代码,我已经在那里定义了索引,我想使用它。工作得很好!谢谢