JeeWoong Lee
JeeWoong Lee

Reputation: 61

How to show list value at Text class in Flutter

Hello this is a simple code that shows list values. All I want to do is to show CourseModel.dummy()'s values. In this code I just want to print its name.

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),
    );
  }
}

The problem is

child: ListView(
            children: [
              ...courseList.map((CourseModel subject) {
                return Text(subject.name);
              }).toList(),
            ],
          ),

it said

Too many positional arguments: 0 expected, but 1 found.

Have no idea how to solve this. In case you might get confused Course model has is this structure.

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,
    );
  }
}

Upvotes: 0

Views: 2272

Answers (2)

Scott Godfrey
Scott Godfrey

Reputation: 671

You're essentially assigning a List<List> . You're courseList is the list you need to assign to children. Just get rid of the square brackets.

children:  courseList.map((CourseModel subject) {
             return Text(subject.name);
          }).toList(),
        

I believe it's because your text widget isn't accepting any positional arguments. Instead of extending text, try:

Widget _body(CourseModel subject) {
  //body code here (return SafeArea())
}

Upvotes: 0

dm_tr
dm_tr

Reputation: 4783

Try this

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

Upvotes: 1

Related Questions