Home > database >  I can not add class instance to the list in another class. in Flutter
I can not add class instance to the list in another class. in Flutter

Time:10-31

I am trying to add class instance to the list but as i try it, it doesn't shown in the list. I checked if i am even taking the input. Yes i am taking the input, it correctly adds to list but it can not add the list in another class which is in another dart file.

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

From my main dart file:

class _MyAppState extends State<MyApp> {
List<Animes> animeler = [];

i have bunch of examples in animeler list as Animes instance. It does show it on screen but when i enter anything. it does not show the new one.

Add class:

class AnimeAdd extends StatefulWidget {

  List<Animes> animes = [];
  AnimeAdd(List<Animes> anime)
  {
      this.animes = animes;
  }

  @override
  State<StatefulWidget> createState() {
    return _AnimeAddState(animes);
  }

}

i can take the input correctly. i know this from printing the animes list. Instance number gets higher every time i take input.

Where i try to take the list in main file:

Row(
  children: [
    Flexible(
      fit: FlexFit.tight,
      flex: 1,
      child: ElevatedButton(
        child: const Text("Tıkla gör"),
        onPressed: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => AnimeAdd(animeler)));

CodePudding user response:

I don't get the problem correctly, but I guess the problem here is the cause :

   List<Animes> animes = [];
    AnimeAdd(List<Animes> anime){
     this.animes = animes;
     }

here you didn't use the anime value from the constructor, to save the constructor anime inside the animes :

List<Animes> animes = [];
    AnimeAdd(List<Animes> anime){
    this.animes = anime;
  }

the this.animes = animes; is like referring to the same object, but this.animes = anime; is a different thing, it will store anime inside animes

  • Related