Home > Back-end >  How to create a custom class for list in flutter? I am getting this error screen
How to create a custom class for list in flutter? I am getting this error screen

Time:05-01

There is an error when I run my app it doesn't show my text and author from the list. But I am following a course recorded 3 years ago he doesn't use required or late but I need to use it because it throws errors of null safety. Here is the image of the app. [1]: https://i.stack.imgur.com/VgNXu.png

// ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart';
import 'quote.dart';

void main() {
  runApp(MaterialApp(
    home: Quotelist(),
  ),);
}

class Quotelist extends StatefulWidget {
  const Quotelist({Key? key}) : super(key: key);

  @override
  State<Quotelist> createState() => _QuotelistState();
}

class _QuotelistState extends State<Quotelist> {

  List<HQuote> listQuotes = [
    HQuote(text: "hello",author: "Nagu"),
    HQuote(text: "hello1",author: "Nagu1"),
    HQuote(text: "hello2",author: "Nagu2"),
    HQuote(text: "hello3",author: "Nagu3"),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey\[200\],
      appBar: AppBar(
        title: Text("Quotes"),
        centerTitle: true,
        backgroundColor: Colors.red\[400\],
      ),
      body: Column(
        children: listQuotes.map((e) => Text('$e.text')).toList(),
      ),
    );
  }
}


//quote.dart file
class HQuote{
  late String text;
  late String author;

 HQuote({required this.text,required this.author});
}

CodePudding user response:

When adding "." on a variable, you have add the curly braces like: ${x.randomThing}

So, change

children: listQuotes.map((e) => Text('$e.text')).toList(),

To:

children: listQuotes.map((e) => Text('${e.text}')).toList(),
  • Related