flutter - 如何在 Flutter 的 Text 类中显示列表值
问题描述
您好,这是一个显示列表值的简单代码。我要做的就是显示 CourseModel.dummy() 的值。在这段代码中,我只想打印它的名字。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:login_page/models/Course_model.dart';
class Text extends StatelessWidget {
List<CourseModel> courseList = [CourseModel.dummy()];
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: this._body,
);
}
}
extension on Text {
Widget get _body => SafeArea(
child: Container(
color: Colors.white,
child: ListView(
children: [
...courseList.map((CourseModel subject) {
return Text(subject.name);
}).toList(),
],
),
),
);
Widget _subjectRow(CourseModel subject) {
return Container(
color: subject.active == true ? Colors.purple : Colors.white,
child: Text(subject.name),
);
}
}
问题是
child: ListView(
children: [
...courseList.map((CourseModel subject) {
return Text(subject.name);
}).toList(),
],
),
它说
Too many positional arguments: 0 expected, but 1 found.
不知道如何解决这个问题。如果您可能会感到困惑,课程模型就是这种结构。
class CourseModel{
final String name;
final int grade;
final String type;
final String department;
final bool active;
CourseModel({this.name,this.department, this.grade, this.type,this.active});
factory CourseModel.dummy(){
return CourseModel(
name: "SW",
department: "SW",
grade: 2,
type: "MUST",
active: false,
);
}
}
解决方案
试试这个
child: ListView.builder(
itemCount: courseList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(courseList[index].name),
),
},
),
推荐阅读
- c++ - 为什么使用 BasedOnStyle: Google 每次我复制/粘贴时都会“添加”一个缩进?
- apache-kafka - 如何设置 Elastic Node APM 分布式跟踪以使用 Kafka 和多个 Node 服务?
- node.js - 带有 NVM 的节点。Debian 9
- html - 全部关闭时,Bootstrap 手风琴头更小
- d3.js - D3.js 缩放时重新定位元素
- ios - 为什么 Objective-C 将完整的 C++ 类型名称作为字符串包含在二进制文件中?我可以摆脱它吗?
- http - 如何在互联网上托管散景框架应用程序?
- mongodb - MongoDB $减去具有持续时间的日期并考虑给定时区的 DST 更改
- windows - 文件差异
- sql - SQL:连接表