Home > other >  Use value from constructor as variable in code (Flutter/Dart)
Use value from constructor as variable in code (Flutter/Dart)

Time:02-15

I am new with Flutter/Dart - started middle January.

I am busy with an integration from Flutter to Google Sheets. Basically, the user will scan their NFC tag and an entry will be created on Google Sheets. The code to send data to Google Sheets comes from Johannes Milke's video.

My code uses a constructor to create various cards in the Flutter app (code below).

class AttendanceCards extends StatefulWidget {
  AttendanceCards({
    Key? key,
    required this.icon,
    required this.requestText,
    required this.tagConfirmationText,
  }) : super(key: key);

  final IconData icon;
  final String requestText;
  final String tagConfirmationText;

  @override
  State<AttendanceCards> createState() => _AttendanceCardsState();
}

requestText = "Clock in" or "Clock out" (used as Text property for with the icon)

tagConfirmationText = "User clocked in successfully" or "User clocked out successfully" (used in the AlertDialog).

Code used to pass data to Google Sheets:

final user = {
    TagDataFields.timestamp: formatter.format(now),
    TagDataFields.tagName: mifare?.identifier,
    TagDataFields.action: <This section is required>,
    TagDataFields.flag: 'none'
};
await GSheetsApi.insert([user]);

How can I use requestText to be inserted into TagDataFields.action?

Any help or pointing me in the right direction will be greatly appreciated.

Thanks!

CodePudding user response:

You can use .widget for get a value of variable of the constructor

Like this: widget.requestText

  • Related