Home > Software engineering >  Why I got : The body might complete normally, causing 'null' to be returned, but the retur
Why I got : The body might complete normally, causing 'null' to be returned, but the retur

Time:09-27

I'm new with flutter I want to display the same list for 20 times but I always got this error : The body might complete normally, causing 'null' to be returned, but the return type, 'List'

this is my code :

List<int> _getRandomBytes(int nboucle) {
      for (int i = 1; i < nboucle; i  ) {
    List<int> listbytes = [
      0XFF,
      0XCA,
      0XAB,
      0XFF,
      0XCA,
      0XFF,
      0XCA,
      0XAB,
      0XFF,
      0XFF,
      0XFF,
      0XCA,
      0XAB,
      0XFF,
      0XCA,
      0XFF,
      0XCA,
      0XAB,
      0XFF,
      0XFF,
      0XCA,
      0XAB,
      0XFF,
      0XFF,
      0XFF,
      0XCA,
    ];
       return listbytes;
      }

  
   

  }

Thanks in advance for your help

CodePudding user response:

doc:

The analyzer produces this diagnostic when a method or function has a return type that’s potentially non-nullable but would implicitly return null if control reached the end of the function.

simple defenition: Your declare non-null function, which is List<int>. but you put the return inside the loop.

it causes your function to probably return null outside the loop. try this:

List<int> _getRandomBytes(int nboucle) {
    List<int> listbytes=[];
    for (int i = 1; i < nboucle; i  ) {
      listbytes = [
        0XFF,
        0XCA,
        ....
      ];
    }
    return listbytes;
  }

CodePudding user response:

you set the return inside the for loop, here what it should be.

List<int> _getRandomBytes(int nboucle) {
late List<int> listbytes;
for (int i = 1; i < nboucle; i  ) {
  listbytes = [
    0XFF,
    0XCA,
    0XAB,
    0XFF,
    0XCA,
    0XFF,
    0XCA,
    0XAB,
    0XFF,
    0XFF,
    0XFF,
    0XCA,
    0XAB,
    0XFF,
    0XCA,
    0XFF,
    0XCA,
    0XAB,
    0XFF,
    0XFF,
    0XCA,
    0XAB,
    0XFF,
    0XFF,
    0XFF,
    0XCA,
  ];
}
return listbytes;

}

  • Related