Home > Software design >  indexOf is red underlined
indexOf is red underlined

Time:01-02

I tried to use indexOf in my code, but indexOf is red underlined and I don't know why...

This is my function:

  void test() {
     indexx = dateArrayTest.indexOf(formattedDate);
  }

And this are my imports:

import 'package:flutter/material.dart';
import 'package:fitnessapp2/main.dart';
import 'package:intl/intl.dart';

This are the variables I use:

  int formattedDate = int.parse(DateFormat('yyyyMMddkkmm').format(date));
  var indexx;
  var dateArrayTest = {
    202201011940,
    202201011941,
    202201011942,
    202201011943,
    202201011944,
    202201011945,
    202201011946,
    202201011947,
  };

Do I need another import?

CodePudding user response:

Your problem is that your dateArrayTest is being declared as a Set<int> which does not have the indexOf method since elements of a Set cannot be requested by index.

If you need the indexOf method, you need to declare dateArrayTest as a List<int> by using [] instead of {}:

  var dateArrayTest = [
    202201011940,
    202201011941,
    202201011942,
    202201011943,
    202201011944,
    202201011945,
    202201011946,
    202201011947,
  ];
  • Related