1

what I am trying to do is generate a GridView with flutter with the index which is defined above, but it said "Undefined name index" for some reason, can you help me with that please.

Here is the code:

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,),);
                  ))
        )

    );
  }
}

The result I expect is generating 1000 rows of gridview using the index I have already defined.

2 Answers 2

2

Replace your build() with this:

@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(),
      ),
    ),
  );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Change your code to use the values from the property items that your are passing.

Example:

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(),
        ),
      ),
    );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.