skip to Main Content

Here is my code:

onPressed: () {
  link = links[sessName.indexOf(str, 0)];
  print('$link.txt');
},

sessName – links array. For example, I will give the first element: 'http://lingvostim.com/LS2018/about'

I need to add a '.txt' to the end of link. I don’t know why string interpolation isn’t working. The console displays only 'http://lingvostim.com/LS2018/about'. I will be glad of your help.

3

Answers


  1. If you try to print your value 'http://lingvostim.com/LS2018/about' directly, it should work fine. Just make sure your links[sessName.indexOf(str, 0)] expression returns expected value.

    Login or Signup to reply.
  2. You should add null-aware operator ‘??’ to assign an empty string to the link variable if index not found. That is code after adding null-aware operator. Also you must define if link variable is String.

    onPressed: () {
      String link = links[sessName.indexOf(str)] ?? '';
      print('${link ?? ''}.txt');
    },
    
    Login or Signup to reply.
  3. Use parentheses with dollar for concatenation

    onPressed: () {
    link = links[sessName.indexOf(str, 0)]??';
    print('${link}.txt');
    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search