Home > Blockchain >  I am passing a string from a stateful widget to stateful widget and I want it to be converted to int
I am passing a string from a stateful widget to stateful widget and I want it to be converted to int

Time:10-09

I am passing a string from a stateful widget to stateful widget, and I want it to be converted to int and store it in a variable so I can use it as a value for my index

        class RecordNumber extends StatefulWidget {
      final String recordName;
      const RecordNumber(this.recordName, {super.key});

      @override
      RecordNumberState createState() => RecordNumberState();
    }

    class RecordNumberState extends State<RecordNumber> {
      // I wanto initialize the variable recordName here as an integer
      // final int index = (the rocordName);
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Text(widget.recordName),
        );
      }
    }

CodePudding user response:

Create a variable in your state class

int recordNameIndex = 0;

In initState you can then parse your string to an int. You have to write widget before recordName, because your variable is in the widget class and not in the state

@override
  void initState() {
    super.initState();
   recordNameIndex = int.parse(widget.recordName);
  }

CodePudding user response:

you can parse a string to an int with int.parse()

in your code it will be like this :

      final int index = int.parse(recordName);

as example:

      int.parse("102") == 102  // true
  • Related