0

On a cats.dart page I have:


    class Cats{
    String name;
    String gender;
    int age;
    
    Cats({this.name, this.gender, this.age});
    
    setData(list){
    for(int i = 0; i < l.length; i++){
    name = list[0];
    gender = list[1];
    age = list[2];
      }
     }
    }

then I have, on a page called kitty.dart:

    import: 'cats.dart';
    
    List<String> cat = ["fluffy","female",3];
    
    class Kitty extends StatefulWidget {
    
      @override
      _Kitty createState() => _Kitty();
    }
    
    class _Kitty extends State<Kitty> {
      Cats kitten;
      String data = '';
    
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Cats'),
          ),
          body:
          Column(
            children: [
              Text(
                data, style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
              FlatButton(
                  onPressed: (){
                    kitten.setData(cat);
                    setState(() {
                      data = kitten.name;
                    });
                  },
                  child: Text('Get Data', style: TextStyle(fontSize: 20),
                  ),
              ),
            ],
          ),
        );
      }
    }

and the usual main.dart:

import: 'kitty.dart';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(),
      debugShowCheckedModeBanner: false,
      home:  Kitty(),
        );
  }
}

...it even looks silly, but I have tried and cannot figure it out...it says that

The method 'setData' was called on null. Receiver: null Tried calling: setData(Instance(length:3) of '_GrowableList')

Any help would be greatly appreciated...

emiliano67
  • 161
  • 1
  • 8

1 Answers1

0

You have to initialize you kitten objct. Just add this line of code in initState

kitten = Cats();
Yakhyo Mashrapov
  • 360
  • 2
  • 13