Reputation: 51
I'm making a class to later instantiate each contact field of a contact book(first name, last name, phone, email, etc) I want to pass a string as a parameter via constructor, but when using this variable inside the widget, it gives an error.
import 'package:flutter/material.dart';
class campoContato extends StatefulWidget {
final String labelName;
const campoContato({Key? key, required this.labelName}) : super(key: key);
@override
State<campoContato> createState() => _campoContatoState();
}
class _campoContatoState extends State<campoContato> {
@override
Widget build(BuildContext context) {
return TextFormField(
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(horizontal: 20),
labelText: widget.labelName, // < ERROR HERE. IT DOES NOT ALLOW USING THE LABEL NAME
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(width: 1, color: Colors.black))),
);
}
}
I'm going to use a class in another part of the code. If anyone can suggest anything, I'd appreciate it!
I already tried to start the variable with final, var, late but it didn't work
Upvotes: 0
Views: 58
Reputation: 63604
You need to remove const
. You will get labelName
on runtime
decoration: InputDecoration( //here
You can check this question about const
Upvotes: 1