首页 > 解决方案 > 如何在 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,
    );
  }
}

标签: flutter

解决方案


试试这个

child: ListView.builder(
    itemCount: courseList.length,
    itemBuilder: (context, index) {
       return ListTile(
          title: Text(courseList[index].name),
       ),
    },
),

推荐阅读