0

My Main.Dart File:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
  home: QuoteList() ,
));
class QuoteList extends StatefulWidget {
  List<String> quotes = [
    "Hello World"
    "Its never possible until its done"
  ];
  @override
  _QuoteListState createState() => _QuoteListState();
}

class _QuoteListState extends State<QuoteList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      appBar: AppBar(
        title: Text("Awesome Quotes "),
        backgroundColor: Colors.redAccent[400],
        centerTitle: true,

      ),
      body: Column(
        children: Q.map()
      ),
    );
  }
}

My Error:

Performing hot reload...
Syncing files to device AOSP on IA Emulator...
lib/main.dart:27:19: Error: The getter 'quotes' isn't defined for the class '_QuoteListState'.
 - '_QuoteListState' is from 'package:flutter_app3/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'quotes'.
        children: quotes.map()
                  ^^^^^^

Please help me with this I followed Net Ninja Videos

I tried and tried fix it but I couldn't fix it. Plus I am new to flutter.

Kulidu David
  • 51
  • 2
  • 2
  • 7

1 Answers1

0

There are multiple problems with your code.

  1. The list quotes, needs to be under class _QuoteListState extends State<QuoteList> { This is because, the way that you have currently set it up, it is a property of the class. To access it you need to use widget.quotes. Instead you can place that code under the line above.

  2. quotes.map alone will not work. Instead a simpler solution ould be the following line [...widget.quotes.map((eachQuote) => Text(eachQuote.toString()))].


Let me explain it. The children argument of Column takes in a list of type Widget. So you need to map each element of that List of Strings which you've named quotes to a Text widget which will then convert that String to a widget. The ... is used to unpack that into a list which we can then pass into the children: property of Column. Here is the completed code:

import 'package:flutter/material.dart';

class QuoteList extends StatefulWidget {
  List<String> quotes = [
    "Hello World"
    "Its never possible until its done"
  ];
  @override
  _QuoteListState createState() => _QuoteListState();
}

class _QuoteListState extends State<QuoteList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      appBar: AppBar(
        title: Text("Awesome Quotes "),
        backgroundColor: Colors.redAccent[400],
        centerTitle: true,

      ),
      body: Column(
        children: [...widget.quotes.map((eachQuote) => Text(eachQuote.toString()))],
      ),
    );
  }
}
Pranav Manoj
  • 69
  • 1
  • 6