skip to Main Content

I just started to learn Flutter and I’m not able to find my error…

I’m trying to display images from a list in a Grid View but I can’t find why the console can’t find the getter ‘room’.

I created a class with a method and a constructor and another data class where are stored the different lists. One of this list depends of the Room class. for now, the other is fixed.

When i call room.room the class room is find but the getter .room not.

Do I miss something?

import 'package:flutter/material.dart';
import 'package:ipictory/datas/datarooms.dart';
import 'package:ipictory/widgets/room.dart';
import 'detail_room.dart';

class DetailRoomTest extends StatefulWidget{

  @override
  DetailRoomTestState createState() => DetailRoomTestState();

}

class DetailRoomTestState extends State<DetailRoomTest>{

  @override
  Widget build(BuildContext context) {
   final room = DataRooms().listeDataRooms;
   final roomImages = DataRooms().imageLiving;

    var size = MediaQuery.of(context).size;
    double width = size.width;
    double heigth = size.height;

    return Scaffold(
      appBar: AppBar(
        title: Text(room.room,         //-------> error: The getter 'room' isn't defined 
                                                for the type 'List<Room>'.
          style: TextStyle(
              color: Colors.white
          ),
        ),
      ),
      body: Column(
        children: [
          GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
              itemCount: roomImages.length,
              itemBuilder: (BuildContext context, int index) {
                return Card(
                    child:Image(
                        image: AssetImage(roomImages[index],)
                    ),
                );
              }
          )
        ],
      ),
    );
  }
}
import 'package:ipictory/widgets/room.dart';

class DataRooms {

  List<Room> listeDataRooms = [

    Room(room: "La salle à manger", inventoryTitle : "Inventaire", inventory: "Inventaire de la salle à manger : 1 table extensible, 4 chaises en simili cuir et 4 chaises en bois rangées dans le placard.", imagePath: "images/control_images/sallemanger.png" , damagesTitle : "Dommages", damages: "impact sur le plateau de la table, chaises en simili griffées à plusieurs endroits", desc: "La salle à manger est composée d'une table en bois extensibles pouvant accueillir 8 personnes." ),
    Room(room: "Salon", inventoryTitle : "Inventaire",inventory: "Inventaire du salon : 1 table basse, 6 coussins bleus en velour, 5 coussins d'assise du canapé, un panier", imagePath: "images/control_images/salon.png" , damagesTitle : "Dommages",damages: "Aucun dommage", desc: "Le salon est composé d'un canapé d'angle en tissu convertible en un lit 2 places et d'une table basse. Une jolie décoration en fait une zone très agréable. La télévision par satelite permet d'occuper les journées de pluie ou les moments de repos. "),
    Room(room: "La cuisine", inventoryTitle : "Inventaire", inventory: "Inventaire de la cuisine : 2 casseroles, 3 poeles, une vaisselle pour 8 personnes, 2 couteaux de cuisine, etc...", imagePath: "images/control_images/cuisine.png" , damagesTitle : "Dommages",damages: "il manque une cuillère", desc: "La cuisine est entièrement équipée. Cafetière à dosette ou à filtre, théière, grille pain, bouilloire, four à gaz, micro onde, refrigérateur, et plaques au gaz. Tout ce qu'il faut pour faire de bons plats. " ),
    Room(room: "La salle d'eau",inventoryTitle : "Inventaire", inventory: "La salle d'eau contient : 1 petit panier, un sèche cheveux", imagePath: "images/control_images/salle_deau.png" , damagesTitle : "Dommages",damages: "aucun dommage", desc: "Salle de douche parfaite"),
    Room(room: "La chambre parentale",inventoryTitle : "Inventaire", inventory: "la chambre parentale contient : un lit 140, 2 tables de chevets, 2 oreillers et une couverture 240x240", imagePath: "images/control_images/chambreparentale.png" , damagesTitle : "Dommages",damages: "Tache de sang sur le matelas", desc: "super chambre parentale"),
    Room(room: "Le dressing",inventoryTitle : "Inventaire", inventory: "le dressing contient une machine à laver le linge, 2 oreillers supplémentaires, ", imagePath: "images/control_images/dressing.png" , damagesTitle : "Dommages",damages: "aucun dommage", desc: "C'est le dressing de la chambre parentale. Pratique pour tout ranger"),
    Room(room: "chambre 1",inventoryTitle : "Inventaire", inventory: "la chambre 1 contient 2 lit 70cm, 2 oreillers et deux couvertures", imagePath: "images/control_images/chambre1.png" , damagesTitle : "Dommages",damages: "aucun dommage", desc: "super chambre pour enfants. C'est la chambre de Luna et Lana"),
    Room(room: "toilettes",inventoryTitle : "Inventaire", inventory: "Les toilettes contiennent une brosse WC", imagePath: "images/control_images/toilettes.png" , damagesTitle : "Dommages", damages: "Aucun dommage" ),
    Room(room: "Terrasse",inventoryTitle : "Inventaire", inventory: "La terrasse contient : 1 table de jardin, 8 chaises de jardin grises, une plancha, un parasol et son pied, un étendoir à linge, etc.", imagePath: "images/control_images/terrasse.png" , damagesTitle : "Dommages",damages: "7 chaises sont cassées" ),
    Room(room: "jardin",inventoryTitle : "Inventaire", inventory: "Le jardin dispose de deux transats ", imagePath: "images/control_images/jardin.png" , damagesTitle : "Dommages",damages: "aucun dommage" ),
  ];

  List imageLiving = [
    "images/lieux/living_room/salon.png",
    "images/lieux/living_room/salon.png",
  ];
}
class Room {
  String room;

  String inventory;

  String? desc;

  String imagePath;

  String damages;

  String inventoryTitle;

  String damagesTitle;


  Room({
    required this.inventoryTitle,
    required this.room,
    required this.inventory,
    this.desc,
    required this.imagePath,
    required this.damages,
    required this.damagesTitle,});

  String getImage() => imagePath;
}

3

Answers


  1. The error basically mean that your room variable is List<Room> and Room. So, as it’s List<Room> you cannot access Like this room.room you need to access particular index and then access .room again like below

    EX:

    final room = DataRooms().listeDataRooms; //List of Room
    Text(
      room.first.room // Access the first item from list
      // room[0].room Accesing with the help of index, you can write any number in bracket.
      // room.last.room Accessing last item from list
    )
    
    Login or Signup to reply.
  2. You have defined room as final room = DataRooms().listeDataRooms; which itself is a List<Room>. (List of Room)

    So you can access it by room[0].room or room.first.room

    Login or Signup to reply.
  3. You need the widget parameter for the room name.

    class DetailRoomTest extends StatefulWidget{
      String room;
      const DetailRoomTest({
        Key? key,
        required this.room,
      }) : super(key: key);
      
      @override
      DetailRoomTestState createState() => DetailRoomTestState();
    
    }
    
    class DetailRoomTestState extends State<DetailRoomTest>{
       late List<String> _roomImages;
      
    
      @override
      void initState() {
        _roomImages = DataRooms().imageLiving;
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        var size = MediaQuery.of(context).size;
        double width = size.width;
        double heigth = size.height;
    
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.room,       
              style: TextStyle(
                  color: Colors.white
              ),
            ),
          ),
          body: Column(
            children: [
              GridView.builder(
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
                  itemCount: _roomImages.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Card(
                        child:Image(
                            image: AssetImage(_roomImages[index],)
                        ),
                    );
                  }
              )
            ],
          ),
        );
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search