'Flutter: List inheritance in classes

Hello guys I am new on flutter and building a new app... I have a problem that I cannot do it... I want to call elements of List from another class, I wrote some code but I could not call to another class please answer me... I need your help...

EXPLAIN OF PROJECT: I have a bottom navigation bar. First Screen 'Details', I want to show 'status' of element of List. Second Screen 'Profile', I want to show 'year' of element of List. First Screen

I have List 'SERVIS_IHALE_DATA' and constructor 'IhaleData'. These are my datas and I want to call them from another class.

The LIST and CONSTRUCTOR:

    const SERVIS_IHALE_DATA = [
      IhaleData(
          id: 'User1',
          brand: 'Volkswagen',
          year: '2011', 
          status: 'Onarım'
         ),
    ];

class IhaleData {
  final String id;

  final String brand;

  final String year;

  final String status;


  const IhaleData(
      {
      this.id,
      this.brand,
      this.year,
      this.status
   });
}

And I have 'ServisKartDetay' class and call from 'ServisKartDetay' class:

class ServisKartDetay extends StatefulWidget {
  final IhaleData ihaleData;

  ServisKartDetay(this.ihaleData);

  @override
  _ServisKartDetayState createState() => _ServisKartDetayState();
}

class _ServisKartDetayState extends State<ServisKartDetay> {




int _selectedIndex = 0;

  final List<Widget> _selectedItem = <Widget>[
    IlanBilgileri(
      IhaleData(),
    ), //ERROR IS HERE
    Container(
      child: Center(
        child: Text('Screen 2'),
      ),
    )
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

 @override
  Widget build(BuildContext context) {
    return Scaffold(body:Container(
           Text(widget.ihaleData.status),
              ),
            bottomNavigationBar: Theme(
    data: ThemeData(
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent),
    child: BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      backgroundColor: Colors.white,
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.details),
          title: Text('Details'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.info),
          title: Text('Profile'),
        ),
      ],
      currentIndex: _selectedIndex,
      selectedItemColor: Colors.red,
      onTap: _onItemTapped,
    ),
  ),
         );
}

this code is working but my problem is starting here because in a way I don't understand it is not working:

OTHER CLASS 'IlanBilgileri':

class IlanBilgileri extends StatefulWidget {
  final IhaleData kartDetay;

  IlanBilgileri(this.kartDetay);

  @override
  _IlanBilgileriState createState() => _IlanBilgileriState();
}

class _IlanBilgileriState extends State<IlanBilgileri> {
  @override
  Widget build(BuildContext context) {
    return Container(child:Text( widget.kartDetay.status == null
                ? widget.kartDetay.status.toString()
                : 'Hello',
         );
  }
}

it is not giving me error or anything else, it returns null... and I did not understand that's why... I am calling from 'IlanBilgileri' to 'ServisKartDetay' as a page of bottom navigation bar... but it is giving me null...

I hope I could explain...



Solution 1:[1]

IlanBilgileri(
  IhaleData(),
), //ERROR IS HERE

You're giving a new instance of the class IhaleData to IlanBilgileri, this instance used the constructor with all the optional parameters as null. Maybe you wanted to pass the parameter widget.ihaleData which have real values.

class _IlanBilgileriState extends State<IlanBilgileri> {
  @override
  Widget build(BuildContext context) {
    return Container(child:Text( widget.kartDetay.status == null
                ? widget.kartDetay.status.toString()
                : 'Hello',
         );
 }

After that in the IlanBilgileri you use a ternary operator evaluates a condition, if it's true it returns the first value, else the second

condition ? first_value : second_value

Here you're asking if(widget.kartDetay.status == null) then return the status as a string, but if it's null then the string is "null", if it's not null then return 'Hello'. As you can imagine the object you pass IhaleData() has all its parameters null because it's an object you created in that moment and you never initialized its values

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EdwynZN