如何为颤振列表创建自定义类?我得到这个错误屏幕



有一个错误,当我运行我的应用程序,它不显示我的文本和作者列表。但我现在用的是三年前录制的课程,他不使用需要或迟到,但我需要使用它,因为它抛出null安全错误。这是这款应用的图片。[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});
}

在添加"在变量上添加花括号,如:${x.randomThing}

所以改变

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

:

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

最新更新